Skip to content

Mocking

Ruslan Fridman edited this page May 16, 2017 · 1 revision

Provided

Mocks can be setup for functions using the provided keyword.

(defn bad-function []
  (throw (js/Error "badness")))

(fact "Mocking"
  (bad-function) => "Success!"
  (provided
    (bad-function) => "Success!"))

Provided will create a mock whose behavior is specified by arrow expressions. The left hand side is the function call to be mocked and the right hand side is the desired return value when the mocked function is invoked. In the above example provided allows us to bypass bad-function and return "Success!" instead of throwing an exception. Provided only effects the assertion that it immediately follows

(fact "Mocking"
  (bad-function) => (throws js/Error)
  (bad-function) => "Success!"
  (provided
    (bad-function) => "Success!"))

In this case the first assertion is not mocking bad-function so it will throw an exception but the second assertion will have a mock.

Mocking With Arguments

Provided is argument aware.

(defn sum [arg1 arg2]
  (+ arg1 arg2))

(fact "Mocking"
  (sum 1 2) => 5
  (provided
    (sum 1 2) => 5))

In this example we are saying that when sum is called with the arguments [1 2] we should return 5.

Clone this wiki locally