vendredi 14 octobre 2016

mocking a function call in midje

Say I have a function

(defn extenal_api_fn [stuff]
   ... do things....
)

(defn register_user [stuff]
  (external_api_fn stuff))

And then a test

(def stuff1
  {:user_id 123 })

(def stuff2
  {:user_id 234})

(background (external_api_fn stuff1) => true
            (with-redefs [external_api_fn (fn [data] (println "mocked function"))]
            (register_user stuff1) => true
            (register_user stuff2) => true)

(facts "stuff goes here"
  (fact "user that registers correctly
    (= 1 1) => truthy)
  (fact "user that has a registration failure"
    (= 1 2) => falsy))

This fails with

"you never said #'external_api_fn" would be called with these arguments:
    contents of stuff2

What would be a good way to stub this function call (in only some cases) in order to simulate an internal transaction failure.

Aucun commentaire:

Enregistrer un commentaire