with-redefs
可以根据需要处理尽可能多的功能。这是我的实际生产测试中的一个经过编辑的示例。
(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 ...
)
如果您需要重新定义要重新定义的另一个函数使用的函数,则必须嵌套对 with-redef 的调用。这有时会导致人们认为 with-redefs 只适用于一个函数。
with-redefs-fn
除非您知道有特定原因,否则您可能不想使用。