Rescuing exceptions in an around hook won’t work because within example.run RSpec rescues the exception as part of tracking the example state, notifying formatters, etc.
Are you looking to track *all* raised exceptions (even if something rescues them and/or retries)? Or just all the example failure exceptions? If you’re looking for the former, you’ll probably have to monkey patch Kernel#raise or use a tracepoint <http://ruby-doc.org/core-2.2.0/TracePoint.html>. If you’re looking for the latter, you can implement a listener/formatter that registers the :example_failed event with RSpec so that it is notified every time an example fails, and then you can do whatever you want when notified. HTH, Myron On Wed, Jul 22, 2015 at 7:20 PM, Dylan Reichstadt <[email protected]> wrote: > Hey Jon, > > Pretty solid idea! I didn't think of this. > > Unfortunately it doesn't look to catch the exception though. Tested > with RSpec::Expectations::ExpectationNotMetError > > This was a snippet of my code (which never printed 'In here!' when the > exception was raised - I tried simply rescue Exception as well): > > RSpec.configure do |config| > config.around(:each) do |example| > begin > example.run > rescue RSpec::Expectations::ExpectationNotMetError > puts 'In here!' > raise > end > end > end > > > > > > On Wednesday, July 22, 2015 at 6:11:44 PM UTC-7, Jon Rowe wrote: >> >> Not specifically, but you can configure an around hook to do what you >> want: >> >> Rspec.configure do |config| >> config.around(:example) do |example| >> begin >> example.run >> rescue Net::ReadTimeout >> # … your code >> raise >> end >> end >> end >> >> Jon Rowe >> --------------------------- >> [email protected] >> jonrowe.co.uk >> >> On Thursday, 23 July 2015 at 11:06, Dylan Reichstadt wrote: >> >> Hey All, >> >> I am looking to add better instrumentation around exceptions thrown in >> rspec. For example, I want to track how many times I get a Net::ReadTimeout >> error from Capybara/Rspec. >> >> In a limited scope, I have been: >> >> - Rescuing this exception >> - Incrementing a global variable >> - Re-raising that exception again (after incrementing) >> >> >> However, this exception can happen across all examples. I don't want to >> wrap every scenario in a Begin & Rescue block - that sounds extremely messy. >> >> Does rspec have something to help with this, or does anyone have any >> ideas I can achieve better tracking of exceptions? I have been googling >> around RSpec and Catching All Exceptions, but can't find anything. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "rspec" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rspec/88d51b4d-2bb3-4391-bffd-476663474a07%40googlegroups.com >> <https://groups.google.com/d/msgid/rspec/88d51b4d-2bb3-4391-bffd-476663474a07%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> >> >> -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/ad47b93c-b440-4591-9af5-98f49b0aad1f%40googlegroups.com > <https://groups.google.com/d/msgid/rspec/ad47b93c-b440-4591-9af5-98f49b0aad1f%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CADUxQmsRCWta2UZf9GCEf2a81BeFSHk4qFwJP7mDEyfGGR_0oQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
