Created
February 12, 2011 01:41
-
-
Save timmc/823389 to your computer and use it in GitHub Desktop.
How should I unit test code that uses globally-defined refs?
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
(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 |
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
; 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)))) |
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
(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