do_not_allow usability issue with "rescue"
wincent opened this issue · 2 comments
I'm currently testing a method which uses "rescue" like this:
def foo
do_something
rescue Exception => e
log(e) # log the exception and swallow it
end
In one of my specs I want to use "do_not_allow" to confirm that something doesn't happen:
it 'should not call thingy' do
do_not_allow(Target).thingy
foo
end
The trouble is, that if "thingy" is called, RR will immediately throw an RR::Errors::TimesCalledError, which will be swallowed up by the "rescue" in the method being tested and never seen by RSpec.
Would it be possible to change the behavior of "do_not_allow", then, to defer evaluation in the same way that "mock" doesn't actually get verified until the end of the RSpec example?
For the meantime, the workaround is to change my "rescue" handler to explicitly check for RR exceptions and propagate them, but this is a bit ugly because it contaminates the code being tested with knowledge about the testing tools:
rescue Exception => e
raise e if e.kind_of?(RR::Errors::RRError)
log(e) # and swallow
end
Cheers,
Wincent
Thanks for the report. Ideally, I would like this operation to fail fast. Would a throw work instead?
I imagine so, because you could throw a very specific symbol which no application will catch "by accident" in the way that we're seeing here (rescuing "Exception" and catching an RR-specific subclass of it).
Cheers,
Wincent