Rebinding a var in a Lazytest test -- beginner

11 views
Skip to first unread message

fuchsd

unread,
Dec 14, 2010, 11:13:38 AM12/14/10
to Clojure
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

Stuart Sierra

unread,
Dec 14, 2010, 1:37:00 PM12/14/10
to Clojure
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

fuchsd

unread,
Dec 15, 2010, 10:45:47 PM12/15/10
to Clojure
Hi Stuart,

Ahh, got it. do-it/expect worked perfectly.

Thanks!
Dan

On Dec 14, 12:37 pm, Stuart Sierra <the.stuart.sie...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages