Skip to content

A minimalist & non-invasive API for mocking in Clojure

License

Notifications You must be signed in to change notification settings

zhming0/mock-clj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-clj

Build Status Clojars Project

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.

Usage

(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.

Features & Example

Track mock's invocation history

(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))))

Mock private functions with ease

(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)

License

Copyright © 2017 Ming

Distributed under the Eclipse Public License.

About

A minimalist & non-invasive API for mocking in Clojure

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published