Created
April 9, 2017 16:51
-
-
Save joshjones/90f65bb11106053240baf5c6d5a4fc2b to your computer and use it in GitHub Desktop.
Stubbing a function using clojure spec
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
;;;;;;;;;;; STUBBING A FUNCTION | |
(defn select-id [id] | |
; real code would connect to db, just simulating here | |
{:col1 (rand-int 101) | |
:col2 (rand-nth ["Joe" "Bob" "Bill" "Sally"])}) | |
(s/def ::col1 nat-int?) | |
(s/def ::col2 (s/with-gen string? #(gen/such-that (complement empty?) (gen/string-alphanumeric)))) | |
(s/def ::db-row (s/keys :req-un [::col1 ::col2])) | |
(s/fdef select-id | |
:args (s/cat :id pos-int?) | |
:ret ::db-row) | |
;(select-id 5) | |
;=> {:col1 37, :col2 "Bill"} | |
; now turn on instrumenting -- function not called, only gen'd | |
(stest/instrument `select-id {:stub #{`select-id}}) | |
;(select-id 5) | |
;=> {:col1 347, :col2 "1rfT3Pw"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment