【问题标题】:Midje provided not stubbing function in Compojure / Ring handlerMidje 在 Compojure / Ring 处理程序中提供了不存根功能
【发布时间】:2015-04-16 04:47:05
【问题描述】:

我正在尝试使用 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”,从而通过单元测试,但是,测试失败,因为调用了真正的实现 - 调用了真正的视图。

【问题讨论】:

    标签: unit-testing clojure ring compojure midje


    【解决方案1】:

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

    【讨论】:

    • 为建议喝彩,但虽然它可能让我更进一步,但它并没有解决我今天发现的核心问题,它是所提供的范围。我会发布一个答案供参考。
    【解决方案2】:

    我今天发现我的范围很混乱 - 引入响应的 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")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 2015-05-08
      • 2014-05-11
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 2011-12-05
      相关资源
      最近更新 更多