Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Issue #123 #147

Merged
merged 1 commit into from Jun 4, 2012

Conversation

Projects
None yet
3 participants
Contributor

Bodhisattva2-0 commented Jun 4, 2012

Improved error message when user forgets to stub a method with default behavior. Based on this definition of 'with'.

"Constrains a stub or message expectation to invocations with specific arguments. With a stub, if the message might be received with other args as well, you should stub a default value first, and then stub or mock the same message using with to constrain to specific arguments."

@dchelimsky dchelimsky and 1 other commented on an outdated diff Jun 4, 2012

lib/rspec/mocks/error_generator.rb
@@ -28,6 +28,13 @@ def raise_unexpected_message_args_error(expectation, *args)
end
# @private
+ def raise_no_stub_with_default_behavior(expectation,*args)
@dchelimsky

dchelimsky Jun 4, 2012

Owner

How about raise_no_default_stub_error or raise_missing_default_stub_error? I think either aligns better with the other raise_xxx_error method names. WDYT?

@Bodhisattva2-0

Bodhisattva2-0 Jun 4, 2012

Contributor

'raise_missing_default_stub_error' sounds better of the lot. I'll make the changes. Thanks.

@dchelimsky dchelimsky commented on an outdated diff Jun 4, 2012

lib/rspec/mocks/proxy.rb
@@ -129,6 +129,8 @@ def message_received(message, *args, &block)
elsif stub = find_almost_matching_stub(message, *args)
stub.advise(*args)
raise_unexpected_message_args_error(stub, *args)
+ elsif expectation = find_almost_matching_satisfied_expectation(message,*args)
+ @error_generator.raise_no_stub_with_default_behavior(expectation, *args) if not null_object?
@dchelimsky

dchelimsky Jun 4, 2012

Owner

This should be wrapped in a local private method to align with the other branches that raise.

Contributor

Bodhisattva2-0 commented Jun 4, 2012

Made both suggested changes.

@dchelimsky dchelimsky commented on the diff Jun 4, 2012

spec/rspec/mocks/bug_report_123_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe 'with' do
+ it "should ask the user to stub a default value first if the message might be received with other args as well" do
+ obj = Object.new
+ obj.should_receive(:foobar).with(1)
@dchelimsky

dchelimsky Jun 4, 2012

Owner

I merged locally after you made my suggested changes. All the specs pass but then when I tried it it didn't actually work as expected yet. If you change this example to use stub instead of should_receive, you'll see it fail.

The reason is that find_almost_matching_stub returns the stub and we never get to the next conditional. I'm thinking we can just delegate this branch to raise_missing_default_stub_error instead of raise_unexpected_message_args_error and that solves the problem. WDYT?

Owner

dchelimsky commented Jun 4, 2012

I'm going to go ahead and take what you've got and go from there. Thanks for the pull!

dchelimsky added a commit that referenced this pull request Jun 4, 2012

Merge pull request #147 from c42engineering/issue123
Issue #123: Improved error message when user forgets to stub a method with a default.

@dchelimsky dchelimsky merged commit 1ebc698 into rspec:master Jun 4, 2012

Owner

dchelimsky commented Jun 4, 2012

I made some adjustments in 49ce496.

This pull request passes (merged 409955f into 4b70b45).

Bodhisattva2-0 pushed a commit to c42engineering/rspec-mocks that referenced this pull request Jun 8, 2012

@alindeman alindeman referenced this pull request Jun 10, 2012

Closed

#with and stubbing #123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment