1
我有一个我想测试的函数。为什么我无法使用-ref提取到单独的函数?
(defn -main
[& args]
(let [port (Integer/parseInt (or (first args) "8080"))]
(run-jetty
app
{:port port})))
我需要模拟的原因很明显走run-jetty
,所以我用with-redefs
验证正确的ARG游戏通过。这很好。
(it "should overide the port with first arg"
(with-redefs [run-jetty (fn [handler opts] [handler opts])]
(should= 8081 (jetty-port (underTest/-main "8081")))))
我想,因为我在一个多试验几次用它来提取with-redefs
电话,所以我想这:
(defn mock-jetty
[test]
(with-redefs [run-jetty (fn [handler args] [handler args])] test))
(describe "The app"
(it "should default to port 8080"
(mock-jetty
(should= 8080 (jetty-port (underTest/-main))))))
此测试调用真正run-jetty
功能,并启动服务器。为什么会发生?
编辑
更改提取到宏:
(defmacro mock-jetty
[someTest]
`(with-redefs [run-jetty (fn [handler args] [handler args])] ~someTest))
(describe "The app"
(it "should default to port 8080"
(mock-jetty
(should= 8080 (jetty-port (underTest/-main))))))
抛出此异常: Can't use qualified name as parameter: boost-bin.handler-test/handler
这将引发异常'了java.lang.RuntimeException:无法使用合格名称作为参数:boost-bin.handler-test/handler'。我是否需要引用函数params? – ZeroDivide
你能展示一个宏调用的例子吗? – leetwinski
当然,我在上面添加了一个修改。 – ZeroDivide