Skip to content

Instantly share code, notes, and snippets.

@joshjones
Created April 9, 2017 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshjones/90f65bb11106053240baf5c6d5a4fc2b to your computer and use it in GitHub Desktop.
Save joshjones/90f65bb11106053240baf5c6d5a4fc2b to your computer and use it in GitHub Desktop.
Stubbing a function using clojure spec
;;;;;;;;;;; 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