Skip to content

Commit

Permalink
fixes #36 by expanding side-effects docs
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Jun 29, 2024
1 parent 05af681 commit b351e0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Only accretive/fixative changes will be made from now on.

* 2.1.next in progress
* Address [#36](https://github.com/clojure-expectations/clojure-test/issues/36) by expanding `side-effects` documentation, and mentioning `with-redefs`.
* Expose `run-test` and `run-test-var` from `clojure.test` (Clojure 1.11).

* 2.1.201 --2024-05-07
Expand Down
21 changes: 21 additions & 0 deletions doc/side-effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ so `my-code` executes the non-truthy path.
In `my-code-test-2`, we mock `my-pred` to return `true` and so `my-code` executes
the truthy path and we can verify that calls `println` as expected.

The `side-effects` macro is deliberately simple: it focuses on the arguments
passed to the mock -- and that is what it returns -- but only allows for a
single return value (either `nil` or the specified value), even if there are
multiple calls to the mocked function. The return value is fixed for all calls,
and does not depend on the arguments passed to the mock. If you need more
complex behavior in a mock, you can use `with-redefs` or similar, and write
your own argument capturing logic, if needed.

For comparison, the following `side-effects` call and the `with-redefs` code
have the same effect:

```clojure
(side-effects [[my-pred true] println] (my-code 42))
;; is equivalent to:
(let [calls (atom [])]
(with-redefs [my-pred (fn [& args] (swap! calls conj args) true)
println (fn [& args] (swap! calls conj args) nil)]
(my-code 42)
@calls))
```

Because `side-effects` can return multiple results, and each result is a sequence
of values, it often helps to combine `side-effects` with `more-of`:

Expand Down

0 comments on commit b351e0f

Please sign in to comment.