Skip to content
marick edited this page Oct 28, 2010 · 18 revisions

In both sweet and semi-sweet styles, Midje allows you to put checker functions on the right-hand side of arrows:

(fact (+ 1 1) => odd?)

By default, they produce these sorts of failure messages:

FAIL at (t_sweet_test.clj:71)
Actual result did not agree with the checking function.
    Actual result: 2
    Checking function: odd?

I've found, though, that custom checker methods often have two steps. First, they transform the actual output, then they check it against an expected value. In such cases, the actual result is often not what you want to see. You want to see the intermediate value.

Here's an example. Suppose I'm writing a Clojure version of the Ruby Sequel library. I might have a test that checks how a clojure-sequel function turns into SQL:

(fact
  (with-connection db
    (dataset :countries) => (contains [{:id 1, :region "North America", :name "USA"}])))

At the moment, contains uses the code-under-test's all function, which executes a query that returns all the results:

(defn contains [expected]
  (fn [actual]
    (= (all actual) expected)))

The test results look like this:

FAIL at (core.clj:40)
Actual result did not agree with the checking function.
    Actual result: {:table :countries}
    Checking function: (contains [{:id 1, :region "North America", :name "USA"}])

This isn't wildly informative about what went wrong. This would be better:

FAIL at (core.clj:38)
Actual result did not agree with the checking function.
        Actual result: {:table :countries}
        Checking function: (contains [{:id 1, :region "North America", :name "USA"}])
During checking, the function applied #'all to the result.
It was surprised to see [{:region "North America", :name "USA"}].

In 0.7, this can be accomplished by creating a "chatty checker", like this:

(def contains (chatty-checker (= (all actual) expected)))

If this turns out to be useful and used, I'd like to expand on the idea somehow.

Clone this wiki locally