Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Created March 23, 2013 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenjamin/7edfefcbf1eaa02d6a58 to your computer and use it in GitHub Desktop.
Save glenjamin/7edfefcbf1eaa02d6a58 to your computer and use it in GitHub Desktop.
Possible approach to stub spies
(def stubs "Keep a track of all calls to stubs" (atom {}))
(defmacro stub
"Replace function with a canned version that records its calls"
[func canned & body]
`(with-redefs [~func (fn stub[& args#]
(swap! (get @stubs ~func) conj args#)
~canned)]
(swap! stubs assoc ~func (atom []))
(try
~@body
(finally (swap! stubs dissoc ~func)))))
(defn stub-calls
"Fetch stub calls for function"
[func] @(get @stubs func))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment