使用let语句中提供的midje不会存根方法 [英] Using midje provided in a let clause doesn't stub methods
问题描述
当我使用一个外部let子句来构造一些定义和调用的测试时,存根不会按照我期望的方式工作。例如:
此测试失败
(let [x(meth1 123)]
x => 246
(提供
(meth2 123)=> 246)))
使用此代码
meth2 [x]
(prnmeth2x)
(* 3 x))
(defn meth1 [x] )]
y))
我不应该使用
let
有midje的声明?我不明白如何让这些通过,而不删除let
。解决方案首先,如果
meth2
在(fn [] x)
返回一个函数,因此事实(fn [] x)=>
其次,我认为
提供
用于存根函数调用只有在执行事实的左/右侧时执行。在你的情况下,(fn [] x)
(和x
已经在那一点评估)246
(这是常量)。meth1
永远不会在您的事实上下文中被调用。
的两件事。或者你让
let
部分在你的事实的左边:...
(let [x(meth1 123)] x)=> 246
...
或者让
x
在测试事实时评估的函数:...
#(meth1123)](x))= 246
...
我不认为有办法真的
提供 >(let [x(meth1 123)]
x => 369
(let [...]
x => 246
(提供
)=> 246))))
A
let
包装事实似乎在触摸第一个事实之前执行。When I make a test using an outer let clause to structure some definitions and calls, the stubs don't work the way I'd expect. For example:
This test fails
(fact "blah" (let [x (meth1 123)] x => 246 (provided (meth2 123) => 246)))
With this code
(defn meth2 [x] (prn "meth2" x) (* 3 x)) (defn meth1 [x] (let [y (meth2 x)] y))
Am I not supposed to use
let
statements with midje? I can't understand how to get these to pass without removing thelet
.解决方案First of all, your test would even fail if
meth2
was stubbed correctly since(fn [] x)
returns a function, thus the fact(fn [] x) => 246
will never hold.Secondly, I think that
provided
is used to stub function calls only when executing the left/right-hand-side of facts. In your case that's(fn [] x)
(andx
is already evaluated at that point), as well as246
(which is constant).meth1
never gets called in the context of your fact, only beforehand.To change that, you could do one of two things. Either you make the
let
part of the left-hand-side of your fact:... (let [x (meth1 123)] x) => 246 ...
Or you make
x
a function that is evaluated when the fact is tested:... (let [x #(meth1 123)] (x)) => 246 ...
I don't think there is a way to really see
provided
in action, i.e. like this:(let [x (meth1 123)] x => 369 (let [...] x => 246 (provided (meth2 123) => 246))))
A
let
wrapping facts seems to be executed before the first fact is touched.这篇关于使用let语句中提供的midje不会存根方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!