Using midje provided in a let clause doesn't stub methods

拈花ヽ惹草 提交于 2020-01-15 05:27:44

问题


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 the let.


回答1:


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) (and x is already evaluated at that point), as well as 246 (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.



来源:https://stackoverflow.com/questions/20234605/using-midje-provided-in-a-let-clause-doesnt-stub-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!