Closed marcomorain closed 3 years ago
I like the idea but not sure exceptions are the best way to implement it.
Generally fns that are stubbed are at least one call away from the test code so it's not hard to imagine a case where intermediate code catches the Exception
raised by with-trap
.
An assert
throws an Error
which is less likely to be intercepted.
Using a failing is
from clojure.test would rule out other testing frameworks :(
Yeah, I'm not sure how best to implement this either (and @cayennes does not like this approach).
Generally fns that are stubbed are at least one call away from the test code so it's not hard to imagine a case where intermediate code catches the Exception raised by with-trap.
It's the call to with-trap
that throws, not the stubbed function, so I don't think this is the case.
It's the call to with-trap that throws, not the stubbed function, so I don't think this is the case.
Ah cool, my bad, carry on :)
Closing this as a stale PR -- if there is still interest, we can rebase against the default branch or open an issue to discuss it.
Add a new macro, with-trap that throws and exception is any of the supplied functions are called.
The rational for addign this macro is to avoid this common pattern when testing Clojure:
This can be replaced with the following:
If foo is not called by bar in this example, then the code will behave as normal and the result of bar will be returned from the bond/with-trap form. If foo is called, then bond/with-trap will throw an exception.