Skip to content

Commit

Permalink
Merge pull request #14 from GreenPowerMonitor/fix/wrong-test-assertion
Browse files Browse the repository at this point in the history
Correcting error in a test assertion and making calls-to throw when c…
  • Loading branch information
andrestylianos authored Apr 9, 2018
2 parents d5a329b + 017e238 commit e6bbb96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ In the following example[<sup>2</sup>](#nota2), we stub the `rand` function to m
(rand)
(catch :default e
(is (= "Too many calls to stub" (ex-message e)))
(is (= {:causes :calls-exceeded, :provided-return-values [1 4 6 3]}
(is (= {:causes :calls-exceeded :provided-return-values [1 4 6 3]}
(ex-data e))))))))
```
#### 1.2. `:constantly`
Expand Down Expand Up @@ -114,8 +114,10 @@ a `nil` will be returned when the received parameters don't match any of the map
### 2. Spying function calls
You can use the `:spying` option inside `with-doubles` macro to spy on all the calls to the functions included in the vector after `:spying` keyword. To check the calls to the spied functions and the arguments passed in each call, you have to use the `calls-to` function.

In the following example, we spy the calls to `some-function` and `println` functions. Then we call twice `some-function` and three times `greetings-function` (which calls `println`). Finally, we use `calls-to` function to check
the calls to each spied function and the arguments passed to them in each call.
In the following example, we spy the calls to `some-function` and `println` functions. Then we call twice `some-function` and three times `greetings-function` (which calls `println`).
Finally, we use `calls-to` function to check the calls to each spied function and the arguments passed to them in each call.

Notice that if we erroneously used `calls-to` on a function that was not being spied, it'd throw an exception to let us know.
```clojure
(ns greenpowermonitor.test-doubles.spying-examples
(:require
Expand All @@ -131,7 +133,7 @@ the calls to each spied function and the arguments passed to them in each call.
(deftest spying-functions
(td/with-doubles
:spying [some-function
println]
println]

(some-function "koko" "triki")
(some-function "miko" "miki")
Expand All @@ -145,7 +147,13 @@ the calls to each spied function and the arguments passed to them in each call.
(is (= ["koko" "triki"] (-> some-function td/calls-to first)))

(is (= 3 (-> println td/calls-to count)))
(is (->> greetings-function td/calls-to (every? #(= % "Hola!"))))))
(is (->> println td/calls-to (every? #(= % ["Hola!"]))))

(try
(td/calls-to greetings-function)
(catch :default e
(is (= "Checking calls to not spied function"
(ex-message e)))))))
```
### 3. Ignoring function calls
You can use the `:ignoring` option inside `with-doubles` macro to ignore all the calls to the functions included in the vector after `:ignoring` keyword.
Expand Down
9 changes: 4 additions & 5 deletions src/greenpowermonitor/test_doubles.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
(reset! *spies-atom* {})))))

(defn calls-to [function]
(let [calls (some-> *spies-atom*
deref
(get function)
deref)]
(mapv vec calls)))
(if-let [calls (some-> *spies-atom* deref (get function) deref)]
(mapv vec calls)
(throw #?(:clj (Exception. "Attempting to check calls for a function that is not being spied on")
:cljs (js/Error. "Attempting to check calls for a function that is not being spied on")))))
13 changes: 11 additions & 2 deletions test/greenpowermonitor/test_doubles_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@
(is (= ["koko" "triki"] (-> some-function td/calls-to first)))

(is (= 3 (-> println td/calls-to count)))
(is (->> greetings-function td/calls-to (every? #(= % "Hola!"))))))
(is (->> println td/calls-to (every? #(= % ["Hola!"]))))

(try
(td/calls-to greetings-function)
(catch #?(:clj Exception
:cljs :default)
e
(is (= "Attempting to check calls for a function that is not being spied on"
#?(:clj (.getMessage e)
:cljs (ex-message e))))))))

(deftest ignoring-functions
(let [double-and-greet (fn [x] (print x) (greetings-function) (* 2 x))]
Expand Down Expand Up @@ -61,7 +70,7 @@
(is (= "Too many calls to stub"
#?(:clj (.getMessage e)
:cljs (ex-message e))))
(is (= {:causes :calls-exceeded, :provided-return-values [1 4 6]}
(is (= {:causes :calls-exceeded :provided-return-values [1 4 6]}
(ex-data e)))))))

(testing "make a function return always the same value"
Expand Down

0 comments on commit e6bbb96

Please sign in to comment.