concept call in category clojure

appears as: calls, call, calls, call, The call, A call
Clojure in Action, Second Edition

This is an excerpt from Manning's book Clojure in Action, Second Edition.

In reality, the defn macro expands to a combination of calls to def and fn, where fn is itself another macro and def is a special form. Here, def creates a var with the specified name and is bound to a new function object. This function has a body as specified in the defn form. Here’s what the equivalent expanded form looks like:

Unit testing is testing at a unit level, which in the case of Clojure is the function. Functions are often composed of other functions, and there are times when testing such upper-level functions that it’s useful to mock out calls to certain underlying functions. Mocking functions is a useful technique (often used during unit testing) where a particular function is replaced with one that doesn’t do anything. This allows you to focus only on those parts of the code where the unit test is being targeted.

In case you wondered why running the same test multiple times doesn’t cause an accumulation in the mock-calls atom, it’s because the call to stub-fn resets the entry for that function. Further, this global state will cause problems if you happen to run tests in parallel, because the recording will no longer correspond to a single piece of code under test. The atom will, instead, contain a mishmash of all calls to various mocks from all the tests. This isn’t what’s intended, so you can fix this by making the state local.

(defmocktest test-filter-greater-than
  (mocking [log-call]
    (let [filtered (expenses-greater-than all-expenses 15.0)]
      (is (= (count filtered) 2))
      (is (= (:amount (first filtered)) 20.0))
      (is (= (:amount (last filtered)) 30.0)))
    (verify-call-times-for log-call 1)
    (verify-first-call-args-for log-call "expenses-greater-than" 15.0)))

The trade-off is that you have to include the calls to your verify macros inside the scope of the call to defmocktest. This is because the mock calls are recorded inside the atom bound by the binding created by the defmocktest macro, and outside such scope there’s nothing bound to *mock-calls*.