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