Midje在Compojure / Ring处理程序中不提供桩存功能 [英] Midje provided not stubbing function in Compojure / Ring handler

查看:260
本文介绍了Midje在Compojure / Ring处理程序中不提供桩存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

内容[](视图/索引))

(defn index [](content))

(defroutes app
)))

并尝试使用

测试

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

(事实它返回响应
(let [response(app(request:get/))]
视图(:body response)=>fake-html
(provided(#'whattodo.handler / content)=>(fn []fake-html))))

我期望stub的函数被调用返回'fake-html',因此单元测试通过,

解决方案

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



工作代码通过此早期测试,而不是使用背景调用

 (事实它返回响应
(对于背景(whattodo.handler /内容)=>fake-html)
))]
(事实渲染索引视图
(:body response)=>fake-html)))


I'm attempting to use Midje to stub the view in a handler unit test, but my use of Midje's (provided) obviously isn't correct.

I've simplified and inlined the view to a (content) function in the handler:

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

(defn content [] (views/index))

(defn index [] (content))

(defroutes app
  (GET "/" [] (index)))

and am trying to test it using

(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")))))

I was expecting the stubbed function to be called returning 'fake-html' and thus the unit test passing, but instead, the test fails as the real implementation is called - invoking the real view.

解决方案

I discovered today that I had my scopes confused - the let block introducing response was outside of the fact call that included the provided. Thus the response was created before the provided was invoked.

Working code which passed this early test instead used the against-background call

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

这篇关于Midje在Compojure / Ring处理程序中不提供桩存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆