Skip to content

Instantly share code, notes, and snippets.

@timmc
Created February 12, 2011 01:41
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 timmc/823389 to your computer and use it in GitHub Desktop.
Save timmc/823389 to your computer and use it in GitHub Desktop.
How should I unit test code that uses globally-defined refs?
(def foo (ref 5))
(def bar (ref 7))
(def stuff (ref nil))
(defn update-stuff! [x]
(dosync
(ref-set stuff (* x @foo @bar))))
; but this is hard to unit-test
; no modification of source code
;-- test code --;
(deftest stuff
(binding [foo (ref 5)
bar (ref 6)
stuff (ref nil)]
(update-stuff! 8)
(is (= @stuff 240))))
(def foo (ref 5))
(def bar (ref 7))
(def stuff (ref nil))
(defn update-stuff!-combinator [r_foo r_bar r_stuff x]
(dosync
(ref-set r_stuff (* x @r_foo @r_bar))))
(defn update-stuff! [x]
(update-stuff!-combinator foo bar stuff x))
;-- test code --;
(deftest stuff
(let [my-stuff (ref nil)]
(update-stuff!-combinator (ref 5) (ref 6) my-stuff 8)
(is (= @my-stuff 240))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment