Permalink
Browse files

Improve on 27059bf.

* Fix specs. The `should_receive(:message).with(/.../)` wasn't working as
  expected...it passed even if the message text didn't match the regex.
  I figured out that it was because of the fact that the error was being
  raised within the example-under-test, which already had two errors and
  was simply printing (or, previously, silencing) the extra error.  The
  solution is to capture the reported message using a stub implementation,
  and then assert on it after running the example.
* The added context ("in an after hook") for 2nd errors happening in an
  after hook wasn't being included in the printed message. This is fixed now.
  • Loading branch information...
1 parent 0b34e42 commit d0cb3242e625c869f673355f0eee6ed3d1501c06 @myronmarston myronmarston committed May 23, 2012
Showing with 17 additions and 7 deletions.
  1. +2 −2 lib/rspec/core/example.rb
  2. +2 −2 lib/rspec/core/example_group.rb
  3. +1 −1 lib/rspec/core/hooks.rb
  4. +12 −2 spec/rspec/core/example_spec.rb
@@ -237,8 +237,8 @@ def instance_eval(&block)
end
# @private
- def instance_eval_with_rescue(&block)
- @example_group_instance.instance_eval_with_rescue(&block)
+ def instance_eval_with_rescue(context = nil, &block)
+ @example_group_instance.instance_eval_with_rescue(context, &block)
end
# @private
@@ -470,12 +470,12 @@ def described_class
# @private
# instance_evals the block, capturing and reporting an exception if
# raised
- def instance_eval_with_rescue(&hook)
+ def instance_eval_with_rescue(context = nil, &hook)
begin
instance_eval(&hook)
rescue Exception => e
raise unless example
- example.set_exception(e)
+ example.set_exception(e, context)
end
end
end
@@ -32,7 +32,7 @@ module AfterHookExtension
include HookExtension
def run(example)
- example.instance_eval_with_rescue(&self)
+ example.instance_eval_with_rescue("in an after hook", &self)
end
def display_name
@@ -266,8 +266,13 @@ def assert(val)
example("e") { raise "example" }
end
- RSpec.configuration.reporter.should_receive(:message).with(/An error occurred/)
+ reported_msg = nil
+ # We can't use should_receive(:message).with(/.../) here,
+ # because if that fails, it would fail within our example-under-test,
+ # and since there's already two errors, it would just be reported again.
+ RSpec.configuration.reporter.stub(:message) { |msg| reported_msg = msg }
group.run
+ reported_msg.should =~ /An error occurred in an around.* hook/i
end
end
@@ -278,8 +283,13 @@ def assert(val)
example("e") { raise "example" }
end
- RSpec.configuration.reporter.should_receive(:message).with(/An error occurred.*after/)
+ reported_msg = nil
+ # We can't use should_receive(:message).with(/.../) here,
+ # because if that fails, it would fail within our example-under-test,
+ # and since there's already two errors, it would just be reported again.
+ RSpec.configuration.reporter.stub(:message) { |msg| reported_msg = msg }
group.run
+ reported_msg.should =~ /An error occurred in an after.* hook/i
end
end
end

0 comments on commit d0cb324

Please sign in to comment.