Simple mocking and stubbing for Clojure unit-tests. Supports Clojure 1.2 through 1.5.
Imagine we had the following functions -
(defn xx [a b]
(defn yy [z]
(defn fn-under-test 
(xx 1 2)
(defn another-fn-under-test 
(+ (xx nil nil) (yy nil)))
Also imagine that we had to test fn-under-test and another-fn-under-test, and we didn’t want to have to deal with the xx or yy functions. Maybe they’re horrible functions that open connections to computers running Windoze or something, I dunno.
Here’s how we might mock them out -
(mocking [xx yy]
(verify-call-times-for xx 1)
(verify-call-times-for yy 1)
(verify-first-call-args-for xx 1 2)
(verify-first-call-args-for yy "blah")))
Pretty straightforward, eh? You just use the
mocking macro, specifying all the functions that need to be mocked out. Then, within the scope of
mocking, you call your functions that need to be tested. The calls to the specified functions will get mocked out (they won’t occur), and you can then use things like verify-call-times-for and verify-first-call-args-for to ensure things worked as expected.
Sometimes your tests need to specify values to be returned by the functions being mocked out. That’s where
stubbing comes in.
Here’s how it works -
(is (= (another-fn-under-test) 30))
(stubbing [xx 1 yy 2]
(is (= (another-fn-under-test) 3))))
So that’s it! Pretty simple. Note how within the scope of
2. Now, for the implementation.
Sometimes you just want to inspect the calls of some function without otherwise interfering with its execution.
(defn my-inc [n]
(inc n)) ;; inc has :inline metadata so cannot be faked
(is (= 43 (my-inc 42)))
(verify-called-once-with-args my-inc 42)))