—UPDATE—
The change I asked for made it into rspec so you can now call example.exception out of the box!
—/UPDATE—
RSpec allows you to run a block of code at the end of each test using the after(:each) method. A change is going to be included in a future (hopefully near future!) version of RSpec that allows you to know what exception the test failed with so that you can decide what to do based on the exception. You’ll be able to do that by asking the example for its exception:
I’ve been using a monkeypatch for a little while that allows me to know if a test passed or failed – I’ve wanted to write out a variable on test failure but until now rspec hasn’t exposed the test result. Here’s the patch:
If you place the above code in your project somewhere, you’ll be able to use it as shown in the following example of its use:
When the next version of RSpec exposes the exception that caused the test to fail, this patch will still work, so it’s fairly well future proof…