Minimalist & non-invasive API for mocking in Clojure.
Overall, it is a syntax sugar around with-redefs
.
It leverages Clojure's unique metadata mechanism to offer common test/assertion utilities for mocking/stubbing.
(with-mock specs & body)
specs => var-symbol value-expr/functions
Temporarily redefines var
while executing body
.
If the right-hand side of a spec is a value, then it will create a function constantly returning the value (stub).
If the right-hand side of a spec is a function, then the var-symbol will temporarily be replaced by the function.
(require ['mock-clj.core :as 'mc])
(defn foo [a] (str "foo" a))
(defn bar [a] (str (foo a) "bar"))
(deftest test-bar
(mc/with-mock [foo "oof"] ; equals to [foo (constantly "oof")]
(is (= (bar "test") "oofbar"))
; Calls history
(is (= (mc/calls foo) [["test"]]))
; Last-call
(is (= (mc/last-call foo) ["test"]))
; call-count
(is (= 1 (mc/call-count foo)))
; Called?
(is (mc/called? foo))))
(ns ns-a)
(defn- g [] true)
(defn f [a] (g))
(ns ns-b)
(deftest mock-private-test
(with-mock [#'ns-a/g "baz"]
(is (= (#'ns-a/f "foo") "baz"))))
(Note: generally, it's not a good idea to mock private functions)
Copyright © 2017 Ming
Distributed under the Eclipse Public License.