Skip to content

Commit 0d389cf

Browse files
authored
Merge pull request #61 from circleci/add-new-assertions-methods
Add called-once-with-args? and called-at-least-once-with-args? to bond/assertions
2 parents 6fe01e6 + c09aa79 commit 0d389cf

File tree

5 files changed

+101
-23
lines changed

5 files changed

+101
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.6.0]
8+
### Added
9+
- New assertions `called-once-with-args?` and `called-at-least-once-with-args?`.
10+
711
## [0.5.0]
812
### Removed
913
- Support for ClojureScript. Please pin to 0.4.0 if you need it.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Bond [![CircleCI Status](https://circleci.com/gh/circleci/bond.png?style=badge)]
44
Bond is a spying and stubbing library, primarily intended for tests.
55

66
```clojure
7-
[circleci/bond "0.5.0"]
7+
[circleci/bond "0.6.0"]
88
```
99

1010
```clojure

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject circleci/bond "0.5.0"
1+
(defproject circleci/bond "0.6.0"
22
:description "Spying library for testing"
33
:license {:name "Eclipse Public License"
44
:url "http://www.eclipse.org/legal/epl-v10.html"}

src/bond/assertions.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
(defn called-with-args?
1515
"An assertion to check if `f` was called with `vargs`.
1616
17-
`vargs` should be a vector of args [args-first-call args-second-call ...] to allow for the checking of multiple calls of `f`."
17+
`vargs` should be a vector of args [args-first-call args-second-call ...] to allow for the checking of multiple calls of `f`.
18+
Note that this method asserts about every call to `f`."
1819
[f vargs]
1920
(= vargs (->> f bond/calls (mapv :args))))
21+
22+
(defn called-once-with-args?
23+
"An assertion to check if `f` was called with `args` strictly once.
24+
25+
`args` should be a vector/coll of args [arg1 arg2 arg3] to compare directly to the value of `:args` from `bond/calls`"
26+
[f args]
27+
(called-with-args? f [args]))
28+
29+
(defn called-at-least-once-with-args?
30+
"An assertion to check if `f` has been called at least once with `args`.
31+
32+
`args` should be a vector/coll of args [arg1 arg2 {:arg3 arg3}] to compare directly to the value of `:args` from `bond/calls`"
33+
[f args]
34+
(boolean (some (fn [call] (= (:args call) args))
35+
(bond/calls f))))

test/bond/assertions_test.clj

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
(deftest called?-works
88
(testing "a spy was called directly"
99
(bond/with-spy [target/foo]
10-
(let [_ (target/foo 1)]
11-
(is (assertions/called? target/foo)))))
10+
(target/foo 1)
11+
(is (assertions/called? target/foo))))
1212

1313
(testing "a spy was called indirectly"
1414
(bond/with-spy [target/foo]
15-
(let [_ (target/foo-caller 1)]
16-
(is (assertions/called? target/foo)))))
15+
(target/foo-caller 1)
16+
(is (assertions/called? target/foo))))
1717

1818
(testing "a spy was not called"
1919
(bond/with-spy [target/foo]
@@ -26,17 +26,17 @@
2626
(deftest called-times?-works
2727
(testing "the number of times a spy was called"
2828
(bond/with-spy [target/foo]
29-
(let [_ (target/foo-caller 1)]
30-
(is (assertions/called-times? target/foo 1 )))
31-
(let [_ (target/foo 2)]
32-
(is (assertions/called-times? target/foo 2)))))
29+
(target/foo-caller 1)
30+
(is (assertions/called-times? target/foo 1))
31+
(target/foo 2)
32+
(is (assertions/called-times? target/foo 2))))
3333

3434
(testing "the number of times a spy was not called"
3535
(bond/with-spy [target/foo]
36-
(let [_ (target/foo-caller 1)]
37-
(is (not (assertions/called-times? target/foo 2))))
38-
(let [_ (target/foo-caller 2)]
39-
(is (not (assertions/called-times? target/foo 1))))))
36+
(target/foo-caller 1)
37+
(is (not (assertions/called-times? target/foo 2)))
38+
(target/foo-caller 2)
39+
(is (not (assertions/called-times? target/foo 1)))))
4040

4141
(testing "called-times? fails when its argument is not spied"
4242
(is (thrown? IllegalArgumentException
@@ -46,18 +46,76 @@
4646
(testing "an assertion for calling a spy with args"
4747
(bond/with-spy [target/foo
4848
target/bar]
49-
(let [_ (target/foo-caller 1)]
50-
(is (assertions/called-with-args? target/foo [[1]]))
51-
(is (not (assertions/called-with-args? target/foo [[2]])))
52-
(is (not (assertions/called-with-args? target/bar [[1]])))
53-
(is (not (assertions/called-with-args? target/foo [[1 2]]))))))
49+
(target/foo-caller 1)
50+
(is (assertions/called-with-args? target/foo [[1]]))
51+
(is (not (assertions/called-with-args? target/foo [[2]])))
52+
(is (not (assertions/called-with-args? target/bar [[1]])))
53+
(is (not (assertions/called-with-args? target/foo [[1 2]])))))
5454

5555
(testing "an assertion for calling a spy multiple times with args"
5656
(bond/with-spy [target/foo]
57-
(let [_ (do (target/foo-caller 1)
58-
(target/foo-caller 2))]
59-
(is (assertions/called-with-args? target/foo [[1] [2]])))))
57+
(target/foo-caller 1)
58+
(target/foo-caller 2)
59+
(is (assertions/called-with-args? target/foo [[1] [2]]))))
6060

6161
(testing "called-with-args? fails when its argument is not spied"
6262
(is (thrown? IllegalArgumentException
6363
(assertions/called-with-args? target/foo [])))))
64+
65+
(deftest called-once-with-args?-works
66+
(testing "an assertion for calling a spy once with args"
67+
(bond/with-spy [target/foo]
68+
(target/foo 1)
69+
(is (assertions/called-once-with-args? target/foo [1]))
70+
(is (not (assertions/called-once-with-args? target/foo [2])))))
71+
72+
(testing "an assertion for calling a spy twice with args"
73+
(bond/with-spy [target/foo]
74+
(target/foo 1)
75+
(target/foo 2)
76+
(is (not (assertions/called-once-with-args? target/foo [1])))
77+
(is (not (assertions/called-once-with-args? target/foo [2])))))
78+
79+
(testing "an assertion for calling a spy indirectly once with args"
80+
(bond/with-spy [target/foo]
81+
(target/foo-caller 1)
82+
(is (assertions/called-once-with-args? target/foo [1]))
83+
(is (not (assertions/called-once-with-args? target/foo [2])))))
84+
85+
(testing "an assertion for a spy that was not called"
86+
(bond/with-spy [target/foo]
87+
(is (not (assertions/called-once-with-args? target/foo [])))))
88+
89+
(testing "called-once-with-args? fails when its argument is not spied"
90+
(is (thrown? IllegalArgumentException
91+
(assertions/called-once-with-args? target/foo [])))))
92+
93+
(deftest called-at-least-once-with-args?-works
94+
(testing "an assertion for calling a spy multiple times"
95+
(bond/with-spy [target/foo]
96+
(target/foo 1)
97+
(target/foo 2)
98+
(is (assertions/called-at-least-once-with-args? target/foo [1]))
99+
(is (assertions/called-at-least-once-with-args? target/foo [2]))
100+
(is (not (assertions/called-at-least-once-with-args? target/foo [3])))))
101+
102+
(testing "an assertion for calling a spy multiple times with the same value"
103+
(bond/with-spy [target/foo]
104+
(target/foo 1)
105+
(target/foo 1)
106+
(is (assertions/called-at-least-once-with-args? target/foo [1]))
107+
(is (not (assertions/called-at-least-once-with-args? target/foo [2])))))
108+
109+
(testing "an assertion for calling a spy once"
110+
(bond/with-spy [target/foo]
111+
(target/foo 1)
112+
(is (assertions/called-at-least-once-with-args? target/foo [1]))
113+
(is (not (assertions/called-at-least-once-with-args? target/foo [2])))))
114+
115+
(testing "an assertion for a spy that was not called"
116+
(bond/with-spy [target/foo]
117+
(is (not (assertions/called-at-least-once-with-args? target/foo [])))))
118+
119+
(testing "called-at-least-once-with-args? fails when its argument is not spied"
120+
(is (thrown? IllegalArgumentException
121+
(assertions/called-at-least-once-with-args? target/foo [])))))

0 commit comments

Comments
 (0)