Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 30e81b56043fe998b8a607e21b9feb3733519697 1 parent c2ea08c
@myronmarston myronmarston authored
View
4 Changelog.md
@@ -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
10 lib/rspec/core/example.rb
@@ -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?
View
47 spec/rspec/core/example_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.