Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for :disallow-mock metadata. #60

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for :disallow-mock metadata.
  • Loading branch information
technomancy committed May 28, 2021
commit 3c70b6ca9073a9b4814d7516714797f97f6a50ba
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -92,6 +92,20 @@ and `with-stub-ns` which can spy/stub every function in a namespace in one go:
(is (= :baz (bar)))))
```

Sometimes you have functions which you want to prevent from being
mocked. You can attach `:disallow-mock` functions to prevent Bond from
mocking these. (They can still be replaced using `with-redefs` of course.)

```clojure
(defn ^:disallow-mock important-fn [x]
(spec-check x)
(send-over-network! x))
```

In the case above, you want to make sure that `send-over-network!`
gets mocked, not `important-fn`, since mocking `important-fn` means
that the tests will not run the spec checks.

Releasing
---------

11 changes: 10 additions & 1 deletion src/bond/james.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
(ns bond.james)

(defn- check-allowed [n v]
(when (and (even? n) (:disallow-mock (meta (resolve v))))
(throw (IllegalArgumentException.
(str "Tried to mock " v ", which is disallowed."))))
v)

(defn spy
"wrap f, returning a new fn that keeps track of its call count and arguments."
[f]
@@ -67,7 +73,9 @@
`(with-redefs ~(->> (mapcat (fn [v]
(if (vector? v)
[(first v) `(spy ~(second v))]
[v `(spy (constantly nil))])) vs)
[v `(spy (constantly nil))]))
vs)
(map-indexed check-allowed)
(vec))
~@body))

@@ -101,6 +109,7 @@
[(first v) `(stub! (var ~(first v))
~(second v))]
[v `(stub! (var ~v) (constantly nil))])) vs)
(map-indexed check-allowed)
(vec))
~@body))

18 changes: 18 additions & 0 deletions test/bond/james_test.clj
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
{:clj-kondo/config {:linters {:private-call {:level :off}
:invalid-arity {:level :off}}}}
(:require [clojure.test :refer (deftest is testing)]
[clojure.stacktrace :as stacktrace]
[bond.james :as bond :include-macros true]
[bond.target-data :as target]))

@@ -107,3 +108,20 @@
(bond/with-stub-ns [[bond.target-data (constantly 3)]]
(is (= 3 (target/foo 10)))
(is (= [10] (-> target/foo bond/calls first :args))))))

(defn ^:disallow-mock no-mock [x]
(+ x 3))

(defn disallowed? [form]
(try (eval form)
false
(catch Exception e
(re-find #"disallowed" (str (stacktrace/root-cause e))))))

(deftest disallow-mock-works
(is (disallowed? `(bond/with-stub [no-mock]
(println (no-mock 33)))))
(is (disallowed? `(bond/with-stub [[no-mock #(+ 2 %)]]
(println (no-mock 33)))))
(is (disallowed? `(bond/with-stub! [[no-mock #(+ 2 %)]]
(println (no-mock 33))))))