Unit testing Midje在Compojure/Ring处理程序中提供了not存根函数

Unit testing Midje在Compojure/Ring处理程序中提供了not存根函数,unit-testing,clojure,ring,compojure,midje,Unit Testing,Clojure,Ring,Compojure,Midje,我试图使用Midje在处理程序单元测试中存根视图,但我使用Midje(提供的)显然是不正确的 我已将视图简化并内联到处理程序中的(内容)函数: (ns whattodo.handler (:use compojure.core) (:require [whattodo.views :as views])) (defn content [] (views/index)) (defn index [] (content)) (defroutes app (GET "/" [] (i

我试图使用Midje在处理程序单元测试中存根视图,但我使用Midje(提供的)显然是不正确的

我已将视图简化并内联到处理程序中的(内容)函数:

(ns whattodo.handler
  (:use compojure.core)
  (:require [whattodo.views :as views]))

(defn content [] (views/index))

(defn index [] (content))

(defroutes app
  (GET "/" [] (index)))
我试着用

(ns whattodo.t-handler
  (:use midje.sweet)
  (:use ring.mock.request)
  (:use whattodo.handler))

(facts "It returns response"
       (let [response (app (request :get "/"))]
         (fact "renders index view" (:body response) => "fake-html"
               (provided (#'whattodo.handler/content) => (fn [] "fake-html")))))

我原以为stubbed函数将被调用,返回“假html”,从而单元测试通过,但相反,测试失败,因为调用了真正的实现-调用了真正的视图。

您不需要函数快捷方式,只需使用
(content)=>…
。现在,midje希望您的代码按字面意思调用
(#content)
,但您的
索引
函数调用
(content)
。您对midje语法的困惑可能是希望将预期结果分配给函数名,但事实并非如此。你必须换掉准确的电话。也就是说,如果您的
index
函数调用
content
时带有一些参数,那么您也必须对此进行解释,例如,通过
(提供的(content“my content”)=>…)

我今天发现我的作用域混淆了-引入响应的let块不在包含所提供内容的事实调用范围之内。因此,响应是在调用提供的之前创建的

通过此早期测试的工作代码使用了后台调用

(facts "It returns response"                                                                                                                          
   (against-background (whattodo.handler/content) => "fake-html")                                                                                 
   (let [response (app (request :get "/"))]                                                                                                       
     (fact "renders index view"                                                                                                                   
           (:body response) => "fake-html")))

为建议喝彩,但尽管它可能让我走得更远,但它并没有解决核心问题,我今天发现这是提供的范围。我将发布一个答案供参考。