Fork me on GitHub

Hi all! I recently finished writing a test using with-redefs, which I think is a pretty awesome piece of Clojure technology. But a colleague mentioned that using with-redefs is frowned upon. As far as I can tell the main concern is that if multiple tests are running at once, then the variable is redefined for all threads. Outside of this, is there much concern? To the best of my knowledge, tests with leiningen are single threaded, so is there a risk? If so, what else should I look into?


My general preference is to abstract all my side-effecting code behind protocols, test the side-effecty impl explicitly, and to reify instances of the protocol for testing code that uses the protocol.


with-redefs is adequate for a fn here or a fn there of course. Abstractions aren’t always worth it.


@surreal.analysis with-bindings is safer, if the vars are dynamic.


And if you care about thread-safety, yes.


Thanks all! I understand the risks, and I’m using it to redefine a function from a library, so I think I’ll stick with it, but I really appreciate the clarification @donaldball and @dottedmag