简体   繁体   English

在Clojure测试中,测试一个功能时如何模拟出多种方法?

[英]In clojure test, how to mock out multiple methods when testing one function?

I am writing a clojure.test unit test for a rather large function in my application. 我正在为我的应用程序中的相当大的功能编写clojure.test单元测试。 This function makes several calls to db and external REST services and does some computation. 该函数对数据库和外部REST服务进行了多次调用,并进行了一些计算。 for example, my function to be tested is like so 例如,我要测试的功能就是这样

(defn myfunc [id]
   (let[
        w (some-security-call id)
        x (some-db-call id)
        y  (some-REST-call x)
        z ( some-audit-call y)

       ]
       (-> y :somekey )))

for the purpose of testing this method, I want to stub out or redefine " some-audit-call " and " some-security-call ". 为了测试此方法,我想存根或重新定义“ some-audit-call ”和“ some-security-call ”。 Clojure's with-redefs-fn only redefines one method at one time. Clojure的with-redefs-fn一次只能重新定义一种方法。

Is there a standad way to mock out multiple functions used in a function being unit tested? 有没有一种标准的方法可以模拟正在单元测试的功能中使用的多个功能?

with-redefs works on as many functions as you want. with-redefs可以根据需要运行许多功能。 Here is a redacted example from my actual production tests. 这是我实际生产测试中的编辑示例。

(with-redefs [namespace/redacted            (constantly [])
              namespace/redacted                 (fn [& args] (async/go namespace/redacted))
              namespace/redacted        (constantly [2 4])
              namespace/redacted                (fn [& args] (namespace/redacted sample-redacted-ads))
              namespace/redacted                (fn [_ _ redacted & _]
                                                  (async/go (cond-> namespace/redacted
                                                              namespace/redacted (dissoc redacted))))
              namespace/redacted                   (fn [& args] (async/go nil))
              namespace/redacted       fake-redacted
              namespace/redacted       fake-redacted
              namespace/redacted      namespace/redacted
              namespace/redacted       (go (constantly []))
              namespace/redacted               (fn [_] [])
              namespace/redacted      namespace/redacted
              namespace/redacted      namespace/redacted
              namespace/redacted                        (fn [_] {:redacted "redacted"})]
  (is (= "redacted"
         (get-in (<!!
                  )
                 ))
      "We should return the redacted if we don't pass it in")
  ... many more tests here ...

)

if you need to redefine a function that is used by another function that you want to redefine then you have to nest the calls to with-redef. 如果需要重新定义要重新定义的另一个函数使用的函数,则必须嵌套对with-redef的调用。 this sometimes causes people to think that with-redefs only works with one function. 这有时会使人们认为with-redef仅适用于一个功能。

You likely don't want to use with-redefs-fn unless you know you have a specific reason. 除非您知道有特定原因,否则您可能不想使用with-redefs-fn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM