Reputation: 96484
I want to output a bunch of information from the test if it fails.
When i puts out the information however it appears before the Failures:
section of the rspec output as opposed to where the specific spec failure info is (line number etc)
Is there a way in rspec for the spec to show the info in the failure itself rather than separately?
I thought mybe an around hook, but...
WARNING: around hooks do not share state with the example the way before and after hooks do. This means that you cannot share instance variables between around hooks and examples.```
Upvotes: 4
Views: 1718
Reputation: 3789
You can use a lambda in your test:
expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: #{detailed info}." }
Will give you output like:
Failures:
1) Blah blah blah
Failure/Error: expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: nil." }
This failed for all sorts of reasons, let me list them.
# ./spec/features/search_results_spec.rb:19:in `block (2 levels) in <top (required)>'
It can be a bit tricky if you have code like expect(x).to eq y.count
as just tacking on the lambda gives 2 params given but 0..1 expected. to get around this use formats like
expect(x).to (eq y.count), lambda { "message" }
Upvotes: 6