Fork me on GitHub
#testing
<
2017-02-14
>
surreal.analysis15:02:08

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?

donaldball16:02:06

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.

donaldball16:02:34

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

dottedmag16:02:57

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

dottedmag16:02:06

And if you care about thread-safety, yes.

surreal.analysis16:02:54

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