Skip to content

Commit

Permalink
Merge pull request #644 from rspec/issue-639
Browse files Browse the repository at this point in the history
Provide a failure message when trying to observe an unimplemented method...
  • Loading branch information
xaviershay committed Mar 29, 2014
2 parents ae52236 + 74ff41f commit ccbcbd5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Bug Fixes:
accessed in `inspect` by providing our own inspect output. (Jon Rowe)
* Fix bug in `any_instance` logic that did not allow you to mock or stub
private methods if `verify_partial_doubles` was configured. (Oren Dobzinski)
* Include useful error message when trying to observe an unimplemented method
on an any instance. (Xavier Shay)

### 3.0.0.beta2 / 2014-02-17
[Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.0.0.beta1...v3.0.0.beta2)
Expand Down
5 changes: 4 additions & 1 deletion lib/rspec/mocks/any_instance/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ def public_protected_or_private_method_defined?(method_name)

def observe!(method_name)
if RSpec::Mocks.configuration.verify_partial_doubles?
raise MockExpectationError unless public_protected_or_private_method_defined?(method_name)
unless public_protected_or_private_method_defined?(method_name)
raise MockExpectationError,
"#{@klass} does not implement ##{method_name}"
end
end

stop_observing!(method_name) if already_observing?(method_name)
Expand Down
8 changes: 6 additions & 2 deletions spec/rspec/mocks/partial_double_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,18 @@ def defined_private_method
end

it 'does not allow a non-existing method to be called on any_instance' do
prevents { expect_any_instance_of(klass).to receive(:unimplemented) }
prevents(/does not implement/) {
expect_any_instance_of(klass).to receive(:unimplemented)
}
end

it 'does not allow missing methods to be called on any_instance' do
# This is potentially surprising behaviour, but there is no way for us
# to know that this method is valid since we only have class and not an
# instance.
prevents { expect_any_instance_of(klass).to receive(:dynamic_method) }
prevents(/does not implement/) {
expect_any_instance_of(klass).to receive(:dynamic_method)
}
end

it 'verifies arity range when receiving a message' do
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def reset_all
end

module VerificationHelpers
def prevents(&block)
def prevents(msg = //, &block)
expect(&block).to \
raise_error(RSpec::Mocks::MockExpectationError)
raise_error(RSpec::Mocks::MockExpectationError, msg)
end
end

Expand Down

0 comments on commit ccbcbd5

Please sign in to comment.