Often, when writing tests, I find myself calling some production services or accessing a db to get some authentic data for use in my tests. Then I create mocks which returns the previously collected data.
Squirrel helps you with
- Squirrelling away values, by instrumenting functions and persisting the input => output pairs.
- Easily creating mocks with previously recorded canned responses.
We want to test foo, but at the very bottom foo relies on bar and baz which call some external services. In the repl we do:
(in-ns app.core)
(recording [bar baz] some-file
(foo user-id))
This will record the observed input => output pairs for the functions bar
and baz
into some-file
which we can use in our tests like this:
(deftest foo-does-what-it-should
(with-recordings [app.core/bar
app.core/baz] some-file
(is (= (foo user-id) {:something "or other"}))))
The data we squirrelled away, at the repl, will now be used to create mocks for bar
and baz
.
Whenever the external services that bar
and baz
rely on change, we can just hit the repl for another recording
session.
Copyright © 2014 Lars Andersen
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.