(with-redefs-fn binding-map func)
Temporarily redefines Vars during a call to func. Each val of
binding-map will replace the root value of its key which must be
a Var. After func is called with no args, the root values of all
the Vars will be set back to their old values. These temporary
changes will be visible in all threads. Useful for mocking out
functions during testing.
Source
(defn with-redefs-fn
"Temporarily redefines Vars during a call to func. Each val of
binding-map will replace the root value of its key which must be
a Var. After func is called with no args, the root values of all
the Vars will be set back to their old values. These temporary
changes will be visible in all threads. Useful for mocking out
functions during testing."
{:added "1.3"}
[binding-map func]
(let [root-bind (fn [m]
(doseq [[a-var a-val] m]
(.bindRoot ^clojure.lang.Var a-var a-val)))
old-vals (zipmap (keys binding-map)
(map #(.getRawRoot ^clojure.lang.Var %) (keys binding-map)))]
(try
(root-bind binding-map)
(func)
(finally
(root-bind old-vals)))))