我是Clojure的新手,在搜索了它之后,我把我的问题转到了这样的社区。
我正在测试一个具有对另一个协议的引用的协议实现(deftype),因此构造函数如下所示:
(deftype FooImpl [^Protocol2 protocol-2]
(function bar [_] ... (.bar2 protocol-2))
) ...在某些条件下也满足调用.bar2函数的要求。
我不能做的事情是检测(conjure.core/instrumenting) --调用.bar2来验证传递的参数(verify-called-once-with-args)。
所以问题是:
(instrumenting [ns/function ;;In normal case with `defn`
????] ;; what to write for .bar2
....)谢谢!
发布于 2018-05-29 19:04:04
对于正常使用或测试/模拟,您可以使用reify来实现协议:
(instrumenting [ns/function]
(ns/function (reify Protocol2
(bar2 [_]
; Your mock return value goes here
42))))您还可以使用atom进行自己的检查。
(instrumenting [ns/function]
(let [my-calls (atom 0)]
(ns/function (reify Protocol2
(bar2 [_]
; Increment the number of calls
(swap! my-calls inc)
; Your mock return value goes here
42)))
(is (= @my-calls 1))))以上假设您使用的是clojure.test,但是任何clojure单元测试库都可以验证您的原子的值。
https://stackoverflow.com/questions/50572207
复制相似问题