Clojure Midje and passing functions to functions

58 Views Asked by At

I have a function take-selector that accepts a function as an argument. I have a function make-selector that "somehow" generates a function and passes it to take-selector.

The code could simply look like this.

(defn take-selector [x])

(defn make-selector [] (take-selector (fn [] 7)))

Now I want to use Clojure Midje to write a test case for make-selector. The test objective is to demonstrate that take-selector is called with a function, which returns 7. In the test case I want to replace take-selector with a stub or a mock.

How can I achieve this? I think, I have to define a fact with a prerequisite. This is what I tried so far:

(facts "test that that a function is passed that produces 7"
      (prerequisite (take-selector anything) => 7)
      (make-selector) => 7)

But that does not yet test anything. The test case will always be correct, even if I make the implementation wrong. In order to check if the test is passed, I want Midje to call anything and see if 7 comes out.

So what I actually want is something this:

(facts "test that that a function is passed that produces 7"
      (prerequisite (take-selector (anything :as passed-function)) => (passed-function)
      (make-selector) => 7)

That is weird and obviously does not work. But How can I achieve such a check? One other idea is to test the value 7 inside the prerequisite. But again I have no idea how to do this.

And finally there is still another problem. The check (make-selector) => 7 would only work, because the implementation of make-selector returns what take-selector creates. But that does not have to be the case. It is only in this very small artificial example code true, but in my real code not.

0

There are 0 best solutions below