Both `describe` and `it` create functions. That is, they expand out
to `(fn [] ...)`. Wrapping `binding` around the creation of a fn has
no effect.
For finer control over where the fn gets created and how failures get
reported, you can use `do-it` and `expect`.
(describe ...
(do-it ...
(binding ...
(expect ...)))
This will produce the results you're looking for.
-Stuart Sierra
clojure.com
On Dec 14, 11:13 am, fuchsd <[email protected]> wrote:
> Howdy,
>
> I asked this on #clojure and posted a question on Stackoverflow
> (http://stackoverflow.com/questions/4424220/how-do-i-rebind-a-var-in-a-
> lazytest-describe-test), but no dice. I'm assuming that it's because
> this is so simple and I'm doing it so incorrectly that nobody can even
> begin to formulate a response. Feel free to tell me to go back and re-
> R all TFMs ;)
>
> First of all, sorry if I am screwing up some terminology; I'm pretty
> new to Clojure. I am trying to write a very simple test using Lazytest
> that depends on a var binding. I can not seem to rebind a var in the
> test file and have the code under test use that binding.
>
> Here is the code I am trying to test:
>
> (ns liar-liar.core
> (:gen-class))
>
> (def *input-file-name*)
>
> (defn parse-input
> "Just print return a var for now..."
> []
> *input-file-name*)
>
> (defn -main [& args]
> (binding [*input-file-name* (first args)]
> (println (parse-input))))
>
> And here is the test:
>
> (ns liar-liar.test.core
> (:use lazytest.describe)
> (:use liar-liar.core))
>
> (binding [*input-file-name* "my-input-file"]
> (describe parse-input "Just returns a var"
> (it "returns a var"
> (= "my-input-file" (parse-input)))))
>
> When I try to run this test, I get this error:
>
> java.lang.IllegalStateException: Var liar-liar.core/*input-file-name*
> is unbound.
>
> Interestingly enough, if I move the binding form:
>
> (ns liar-liar.test.core
> (:use lazytest.describe)
> (:use liar-liar.core))
>
> (describe parse-input "Just returns a var"
> (it "returns a var"
> (binding [*input-file-name* "my-input-file"]
> (= "FAIL" (parse-input)))))
>
> The test works as it should, but the reporting isn't ideal, as it
> doesn't print the value of the (parse-input) expression (the test
> passes if I replace "FAIL" with "my-input-file" though):
>
> FAILURE: Namespaces liar-liar.test.core #'liar-liar.core/parse-input
> Just returns a var returns a var
> at liar_liar/test/core.clj line 7
> Expression: (binding [*input-file-name* my-input-file] (= FAIL (parse-
> input)))
> Result: false
> Local bindings:
> {}
>
> Is there some other way I should be going about doing this kind of
> testing?
>
> Thanks! Dan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en