Testing Clojure-如何在实现中插入另一个协议
我是Clojure的新手,在寻找它之后,我把我的问题转向了SO社区 我正在测试一个协议实现(Testing Clojure-如何在实现中插入另一个协议,testing,clojure,Testing,Clojure,我是Clojure的新手,在寻找它之后,我把我的问题转向了SO社区 我正在测试一个协议实现(deftype),它引用了另一个协议,所以构造函数如下所示: (deftype FooImpl [^Protocol2 protocol-2] (function bar [_] ... (.bar2 protocol-2)) ) ..是一些条件太过满足,无法调用.bar2函数 我不能做的事情是插入(concure.core/instrumenting)调用.bar2,以验证传递的参数(ver
deftype
),它引用了另一个协议,所以构造函数如下所示:
(deftype FooImpl [^Protocol2 protocol-2]
(function bar [_] ... (.bar2 protocol-2))
)
..
是一些条件太过满足,无法调用.bar2
函数
我不能做的事情是插入(concure.core/instrumenting
)调用.bar2
,以验证传递的参数(verify called one with args
)
所以问题是:
(instrumenting [ns/function ;;In normal case with `defn`
????] ;; what to write for .bar2
....)
谢谢 对于正常使用或测试/模拟,您可以使用
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单元测试库都可以验证atom的值。谢谢,这很有用!