Skip to content
marick edited this page Dec 30, 2010 · 36 revisions

Midje Facts can have functions on the right-hand side of assertions. Like this:

    (fact (f 33) => even?)

The function is called with the result of the left-hand side. Checkers can also be used to check which arguments apply when trying to match a prerequisite:

    (fact 
      (f 33) => 3
      (provided
         (g even?) => 3))

The rest of this page describes the simpler predefined checkers. See also checkers for collections and strings.

  • truthy

    (fact (f) => truthy or (provided (f truthy) => 3)

    Use truthy? when you don't care about a particular truth value, just that it's neither nil nor false.

  • falsey

    (fact (f) => falsey or (provided (f falsey) => 3)

    Use falsey? when you care that the value is either nil or false, but don't care which.

  • anything

    (fact (f) => anything or (provided (f anything) => 3

    anything is usually used for "don't care" arguments in prerequisites but you could also use it to say that you make no assertions about the result of a function. irrelevant is a synonym.

  • (exactly fn**)**

    (fact (function-generator 3) => (exactly odd?) or (provided (f (exactly odd?)) => 3)

    Use exactly when you want to talk about a particular function as a value, rather than applying it as a checker.

  • (throws class message )

    fact (explosion) => (throws Exception "boom!")

    Use throws when you expect the function to raise an exception. The message argument is optional.

Deprecated checkers

The following are deprecated in favor of the checkers for collections and strings.

  • (in-any-order seq**)**

    (fact (f) => (in-any-order [1 2 3]) or (provided (f (in-any-order 1 2 3]) => 3)

    Use in-any-order when a seq result is to contain a particular set of values, but you don't care about their order.

  • (map-containing expected-hashmap )

    Accepts a map that contains all the keys and values in expected-hashmap. Other keys and values are allowed. (map-containing {:a 1 :b 2}) would accept {:a 1 :b 2} and also {:a 1 :b 2 :c 3}.

  • (maps-containing map-or-maplist )

    Each map in the argument(s) contains contains some map in the actual result. There may be extra maps in the actual result. (maps-containing {:a 1 :b 2} would consider [ {:a 1 :b 2 :c 3} ] a match. It would also accept [ {:a 1 :b 2} {:something :else}]

    maps-containing can be called in two ways. It can be given N arguments, or it can be given a sequence of arguments:

    (maps-containing {:a 1 :b 2} {:a 3 :b 4})
    
    (maps-containing [ {:a 1 :b 2} {:a 3 :b 4}) ]
  • (only-maps-containing map-or-maplist )

    Each map in the argument(s) contains contains some map in the actual result. There may be no extra maps in the actual or expected result. (only-maps-containing {:a 1 :b 2} would consider [ {:a 1 :b 2 :c 3} ] a match. It would not accept [ {:a 1 :b 2} {:something :else}]

    only-maps-containing can be called in two ways. It can be given N arguments, or it can be given a sequence of arguments:

    (only-maps-containing {:a 1 :b 2} {:a 3 :b 4})
    
    (only-maps-containing [ {:a 1 :b 2} {:a 3 :b 4}) ]
Clone this wiki locally