-
Notifications
You must be signed in to change notification settings - Fork 129
Specifying call counts
Since most Clojure functions always return the same value for the same arguments, Midje (by default) doesn't care how often a function-under-test calls its prerequisites. For example, in the following called
is called three times. That's fine by Midje: as long as a prerequisite is called at least once, it checks out.
(fact
(do (called 1) (called 1) (called 1)) => 1
(provided
(called 1) => 1))
In some cases, though, you want finer control over how often a prerequisite must be called. If you want a function called exactly once, but no more, specify it like this:
(fact
...
(provided
(called 1) => 1 :times 1))
You can also give a collection of integers. The fact checks out if that collection includes the number of times the prerequisite was actually called.
(fact
...
(provided
(called 1) => 1 :times (range 2 8)))
Just for grins, you can also give a function that's called with the number of actual prerequisite calls:
(fact
...
(provided
(called 1) => 1 :times even?))
Prerequisites don't just say that a function was called. They say that it was called with particular arguments. If you don't care about the arguments, you can use the anything
checker:
(fact
...
(provided
(called anything) => 1 :times even?))
One of the common uses of :times
is to claim that a prerequisite is never called. In that case, the notation is slightly awkward:
(fact
....
(provided
(called anything) => 1 :times 0))
This says that the function-under-test does not call called
, but if it did, called
would produce 1. That's silly, but it's an unfortunate consequence of how focused Midje is on arrows.