Expectations

a minimalist's unit testing framework

This project is maintained by seancorfield and jaycfields

expectations

adding signal, removing noise

Testing Side Effects

side-effects

expectations 2.0 introduces the 'side-effects macro, which allows you to capture arguments to functions you specify. Previous to version 2.0 behavior was often tested using the 'interaction macro. expectations 2.0 removes the 'interaction macro and embraces the idea of verifying interactions at the data level, using the same type of comparison that is used for all other data.

Examples:

(expect [["/tmp/hello-world" "some data" :append true]
         ["/tmp/hello-world" "some data" :append true]]
  (side-effects [spit]
            (spit "/tmp/hello-world" "some data" :append true)
            (spit "/tmp/hello-world" "some data" :append true)))
In the above example, you specify that spit is a side effect fn, and the 'side-effects macro will return a list of all calls made, with the arguments used in the call. The above example uses simple equality for verification.

(expect empty?
  (side-effects [spit] "spit never called"))
The above example demonstrates how you can use non-equality to verify the data returned.

(expect ["/tmp/hello-world" "some data" :append true]
  (in (side-effects [spit]
                (spit "some other stuff" "xy")
                (spit "/tmp/hello-world" "some data" :append true))))
Immediately above is an example of combining 'side-effects with 'in for a more concise test. Here we're testing that the expected data will exist somewhere within the list returned by 'side-effects

combining side-effects and more-of

It's fairly common to expect some behavior where you know the exact values for some of the args, and you have something general in mind (or you'd like to ignore) the additional args. By combining 'side-effects and 'more-of you can easily destructure a call into it's args and verify whatever you care to..

note: if you haven't read Expect More, you'll probably want to do that first.

(expect (more-of [path data action {:keys [a c]}]
                 String path
                 #"some da" data
                 keyword? action
                 :b a
                 :d c)
  (in (side-effects [spit]
        (spit "/tmp/hello-world" "some data" :append {:a :b :c :d :e :f}))))
The above test is a bit much to swallow at first glance; however, it's actually very straightforward once you've gotten used to the 'more-of syntax. In the above example the 'spit fn is called with the args "/tmp/hello-world", "some data" :append {:a :b :c :d :e :f}. Using 'more-of, we destructure those args, and expect them individually. The path arg is expected to be of type String. The data arg is expected to be a string that matches the regex #"some da". The action is expected to be a 'keyword?. Finally, the options map is destructured to it's :a and :c values, and equality expected.