Skip to content

Instantly share code, notes, and snippets.

@alexanderjamesking
Last active April 29, 2018 09:53
Show Gist options
  • Save alexanderjamesking/091d4e190ca35221e6922653e4a139a4 to your computer and use it in GitHub Desktop.
Save alexanderjamesking/091d4e190ca35221e6922653e4a139a4 to your computer and use it in GitHub Desktop.
(ns spy-example.core-test
(:require [clojure.test :refer [deftest testing is]]
[spy.core :as spy]))
(def beatle->email
{:john "[email protected]"
:paul "[email protected]"
:george "[email protected]"
:ringo "[email protected]"})
(defn lookup-email [beatle-id]
(get beatle->email beatle-id))
(defn send-message [email message]
(println (str "Sending " message " to " email))
nil)
(defn email-beatle [beatle-id message]
(when-let [email (lookup-email beatle-id)]
(send-message email message)))
(deftest email-beatle-test
(testing "A message is sent to a Beatle"
;; example 1 - wrap the original fn (so it is still called)
(with-redefs [send-message (spy/spy send-message)]
(email-beatle :ringo "Hello Ringo!")
(is (spy/called-once? send-message))
(is (spy/called-with? send-message
"[email protected]"
"Hello Ringo!"))))
(testing "A message is not sent to a Rolling Stone"
;; example 2 - call spy without passing a fn (to avoid sending the email)
(with-redefs [send-message (spy/spy)]
(email-beatle :mick "Hello Mr Jagger!")
(is (spy/not-called? send-message)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment