1

我正在尝试使用 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")))))

我期待调用存根函数返回'fake-html',因此单元测试通过,但是,测试失败,因为调用了真正的实现 - 调用了真实的视图。

4

2 回答 2

1

您不需要功能快捷方式,只需使用(content) => .... 正如您现在所拥有的,midje 期望您的代码按字面意思调用(#content),但您的index函数调用(content)。您对 midje 语法的困惑可能是您希望将预期结果分配给函数名称,但事实并非如此。您必须替换确切的呼叫。即,如果你的index函数会content用一些参数调用,你也必须考虑到这一点,例如通过(provided (content "my content") => ...)

于 2015-02-15T21:07:25.943 回答
1

我今天发现我的范围很混乱 - 引入响应的 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")))
于 2015-02-16T20:36:13.627 回答