Permalink
Browse files

Ignore mock expectation failures when the example has already failed.

Mock expectation failures have always been ignored in this situation,
but due to my changes in 27059bf it was printing a confusing message.

Closes #651.
  • Loading branch information...
1 parent c2ea08c commit 30e81b56043fe998b8a607e21b9feb3733519697 @myronmarston myronmarston committed Jul 18, 2012
Showing with 41 additions and 20 deletions.
  1. +4 −0 Changelog.md
  2. +8 −2 lib/rspec/core/example.rb
  3. +29 −18 spec/rspec/core/example_spec.rb
View
@@ -8,6 +8,10 @@ Bug fixes
rspec-expectations. (Myron Marston)
* Fix typo in --help message printed for -e option. (Jo Liss)
* Fix ruby warnings. (Myron Marston)
+* Ignore mock expectation failures when the example has already failed.
+ Mock expectation failures have always been ignored in this situation,
+ but due to my changes in 27059bf1 it was printing a confusing message.
+ (Myron Marston).
### 2.11.0 / 2012-07-07
[full changelog](http://github.com/rspec/rspec-core/compare/v2.10.1...v2.11.0)
View
@@ -205,7 +205,7 @@ def around_each_hooks
# Used internally to set an exception in an after hook, which
# captures the exception but doesn't raise it.
def set_exception(exception, context=nil)
- if @exception
+ if @exception && context != :dont_print
# An error has already been set; we don't want to override it,
# but we also don't want silence the error, so let's print it.
msg = <<-EOS
@@ -301,13 +301,19 @@ def run_before_each
def run_after_each
@example_group_class.run_after_each_hooks(self)
- @example_group_instance.verify_mocks_for_rspec
+ verify_mocks
rescue Exception => e
set_exception(e, "in an after(:each) hook")
ensure
@example_group_instance.teardown_mocks_for_rspec
end
+ def verify_mocks
+ @example_group_instance.verify_mocks_for_rspec
+ rescue Exception => e
+ set_exception(e, :dont_print)
+ end
+
def assign_generated_description
return unless RSpec.configuration.expecting_with_rspec?
if metadata[:description].empty? and !pending?
@@ -259,37 +259,48 @@ def assert(val)
end
end
- context "when the example and an around hook raise errors" do
- it "prints the around hook error rather than silencing it" do
- group = RSpec::Core::ExampleGroup.describe do
- around(:each) { |e| e.run; raise "around" }
- example("e") { raise "example" }
- end
-
+ context 'when the example raises an error' do
+ def run_and_capture_reported_message(group)
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
+ reported_msg
+ end
+
+ it "prints any around hook errors rather than silencing them" do
+ group = RSpec::Core::ExampleGroup.describe do
+ around(:each) { |e| e.run; raise "around" }
+ example("e") { raise "example" }
+ end
+
+ message = run_and_capture_reported_message(group)
+ message.should =~ /An error occurred in an around.* hook/i
end
- end
- context "when the example and an after hook raise errors" do
- it "prints the after hook error rather than silencing it" do
+ it "prints any after hook errors rather than silencing them" do
group = RSpec::Core::ExampleGroup.describe do
after(:each) { raise "after" }
example("e") { raise "example" }
end
- 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
+ message = run_and_capture_reported_message(group)
+ message.should =~ /An error occurred in an after.* hook/i
+ end
+
+ it 'does not print mock expectation errors' do
+ group = RSpec::Core::ExampleGroup.describe do
+ example do
+ foo = mock
+ foo.should_receive(:bar)
+ raise "boom"
+ end
+ end
+
+ message = run_and_capture_reported_message(group)
+ message.should be_nil
end
end
end

0 comments on commit 30e81b5

Please sign in to comment.