Reputation: 343
I use simulation-style tests in to ensure that my entire application works correctly. The core Clojure test library is used for tests, executed via Leiningen. As the -main
function runs, it defines symbols for later use within its logic. The problem is that if I accidentally use a symbol created in one -main
test but never defined in the current -main
test, it still has a value. I would expect to get an error that the symbol is undefined, but it seems my test environment is somehow sharing state between deftest
executions. How can I deal with this? Move all my convenience-driven symbol definitions to a let
statement?
Upvotes: 3
Views: 63
Reputation: 26446
If you are def
-ing global vars inside your function, that's generally considered bad practice and reason enough to use let
as you suggest instead.
However, you can capture a snapshot the mappings of your namespace.
(def ns-snapshot (ns-map *ns*))
So that after you intern symbols
(def foo 1)
(def bar 2)
You can determine the additions
(reduce dissoc (ns-map *ns*) (keys ns-snapshot))
;=> {bar #'so.core/bar, foo #'so.core/foo}
And un-map them
(doseq [[k v] (reduce dissoc (ns-map *ns*) (keys ns-snapshot))] (ns-unmap *ns* k))
So that you'll get the desired undefined error again
foo ;=> CompilerException ... Unable to resolve symbol: foo in this context
Upvotes: 5