Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make have_received complain if passed a block #364

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rspec/mocks/example_methods.rb
Expand Up @@ -114,6 +114,7 @@ def hide_const(constant_name)
# # You can also use most message expectations:
# expect(invitation).to have_received(:accept).with(mailer).once
def have_received(method_name)
warn "have_received ignores its block argument. Called from #{caller[0]}" if block_given?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see I think this is worth merging into 2-14... just as an FYI /cc @myronmarston @samphippen

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of a weird message though: have_received won't ignore its block argument in 3.x, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave 2-14 as is and do things with blocks in 3-0. If we raise this warning now, nobody will expect the reintroduction in 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean toward that too. I think the best options are to either choose to classify this as a bug and "fix" it entirely in 2.14, or wait until 3.x to introduce it as a feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, upon balance, I'd classify this as a feature and add it in 3-0. @alindeman @JonRowe @timcowlishaw what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very happy with that. I started looking at implementing it this morning, but it still needs a little more work. will open a PR shortly!

Thanks,

Tim

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! If you need a hand tomorrow let me know and I'm happy to help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @samphippen and @alindeman: this is a new feature, and adding a warning in 2.14 will add confusion. (Plus I want to focus on 3.0 and stop working on 2.14!)

Matchers::HaveReceived.new(method_name)
end

Expand Down
9 changes: 9 additions & 0 deletions spec/rspec/mocks/matchers/have_received_spec.rb
Expand Up @@ -60,6 +60,15 @@ module Mocks
}.to raise_error(/0 times/)
end

it "warns when a block is used to match the arguments" do
dbl = double_with_met_expectation(:expected_method)
self.stub(:warn => nil)
expect(dbl).to have_received(:expected_method) { }; line = __LINE__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice hack for avoid calculating the __LINE__ offset :)

file = __FILE__
message = /have_received ignores its block argument. Called from #{file}:#{line}/
expect(self).to have_received(:warn).with(message)
end

context "with" do
it 'passes when the given args match the args used with the message' do
dbl = double_with_met_expectation(:expected_method, :expected, :args)
Expand Down