New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add #passed? and #failed? methods to ExampleGroup #401
Comments
You can do this right now: after(:each) do
if example.execution_result[:status] == 'failed'
# do stuff
end
end I'm open to a more direct API, but probably on the example itself. Something like: after(:each) do
if example.failed?
# do stuff
end
end |
On the example itself makes sense. I just tried the following: describe "something" do
it "should pass" do
1.should == 1
end
it "should fail" do
1.should == 2
end
after(:each) do
puts "FAILED!!!" if example.execution_result[:status] == 'failed'
end
end ...but I didn't see the "FAILED!!!" that I was expecting to see in the output. Here's what I got:
What am I missing? |
Playing with execution_result[:status], when I replace the after block of the above example with the following... after(:each) do
p example.execution_result[:status]
end ...all I see in the output is 'nil's:
That's not right... |
@natritmeyer - I was wrong about the timing - sorry for sending you in the wrong direction. The after(:each) do
if example.instance_variable_get("@exception")
# do something
end
end |
@dchelimsky That's exactly what the aforementioned monkeypatch does. Thanks! For reference, here it is: class RSpec::Core::ExampleGroup
def passed?
example.instance_variable_get(:@exception).nil?
end
def failed?
!passed?
end
end |
Again, the example isn't really finished until after all the After a bit more thought, one mechanism that is supported right now is custom formatters. You can use an arbitrary number of formatters, so you are free to do something like this: class FailureReporter
def example_failed(example)
p "#{example.full_description} failed: #{example.execution_result[:exception].message}"
end
def method_missing(m, *a, &b)
# ignore other messages
end
end
RSpec.configure do |c|
c.add_formatter FailureReporter
end
describe "something" do
it "does something" do
raise "oops, something went wrong"
end
end
Does that satisfy your need? |
An interesting idea, but a formatter will apply to all the scenarios being executed, not just one So an example isn't finished until the |
Instance variables are not available to formatters.
But then you'd get a false negative if the failure occurs in a subsequent after block. That might be OK for your situation, but it's not OK for a general purpose addition to the core API. What if we add an accessor for the after(:each) do
if example.exception
# do something
end
end WDYT? |
Interesting. Thinking about the false-fail aspect of it, I guess the Exposing the exception would work. You're right - it's not as pretty as |
I often want to diagnostic stuff in the after(:each) block of a failing test, but there is no way to determine the outcome of a test. As described here: https://github.com/cucumber/cucumber/wiki/Hooks, cucumber has the following construct:
I have put together a monkey patch which lets me do that in rspec. The gist is here:
https://gist.github.com/1009181
It provides #passed? and #failed? methods on RSpec::Core::ExampleGroup
It would be great if rspec provided something like this.
The text was updated successfully, but these errors were encountered: