Last active
April 29, 2018 09:53
-
-
Save alexanderjamesking/091d4e190ca35221e6922653e4a139a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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