Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhming0/mock-clj
A minimalist & non-invasive API for mocking in Clojure
https://github.com/zhming0/mock-clj
clojure mocking testing
Last synced: 4 months ago
JSON representation
A minimalist & non-invasive API for mocking in Clojure
- Host: GitHub
- URL: https://github.com/zhming0/mock-clj
- Owner: zhming0
- License: epl-1.0
- Created: 2017-01-10T04:03:26.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-20T04:29:00.000Z (about 2 years ago)
- Last Synced: 2024-04-24T01:02:26.401Z (10 months ago)
- Topics: clojure, mocking, testing
- Language: Clojure
- Size: 15.6 KB
- Stars: 13
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mock-clj
[![Build Status](https://travis-ci.org/zhming0/mock-clj.svg?branch=master)](https://travis-ci.org/zhming0/mock-clj)
[![Clojars Project](https://img.shields.io/clojars/v/mock-clj.svg)](https://clojars.org/mock-clj)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
```clojure
(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
```clojure
(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
```clojure
(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.