El uso de midje proporcionado en una cláusula let no agrega métodos
Frecuentes
Visto 400 veces
3
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:
Esta prueba falla
(fact "blah"
(let [x (meth1 123)]
x => 246
(provided
(meth2 123) => 246)))
Con este código
(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 Respuestas
4
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 ejecución the left/right-hand-side of facts. In your case that's (fn [] x)
(y 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
...
O haces 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 ver 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.
respondido 27 nov., 13:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas clojure midje or haz tu propia pregunta.
This is what I ended up going with. Along with using contexts for setup and cleanup, instead of a let statement - aciniglio