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

Allow multiple message allowances/expectations via `receive_messages` #399

Merged
merged 28 commits into from Sep 12, 2013

Conversation

Projects
None yet
5 participants
@JonRowe
Member

JonRowe commented Aug 9, 2013

Further to #368 this allow multiple message allowances/expectations via receive_messages.

Show outdated Hide outdated lib/rspec/mocks/targets.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Aug 12, 2013

Member

As promised, refactored to use the improvements from #401

Member

JonRowe commented Aug 12, 2013

As promised, refactored to use the improvements from #401

Show outdated Hide outdated lib/rspec/mocks/framework.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/syntax.rb Outdated
Show outdated Hide outdated lib/rspec/mocks/targets.rb Outdated
@message_return_value_hash.each do |message, value|
yield host, message, value
end
end

This comment has been minimized.

@myronmarston

myronmarston Aug 13, 2013

Member

This is much more readable/understandable than the map_on thing you had before. Thanks!

One suggestion, though: given that the host argument is just yielded back to the caller, it seems a bit unnecessary. The name of this method also suggests that this method does something with it or uses it to determine what messages to yield.

Is there a reason you chose to pass it through? I'd probably (slightly) favor just putting a host = blah line at the call sites and then change this to each_message.

@myronmarston

myronmarston Aug 13, 2013

Member

This is much more readable/understandable than the map_on thing you had before. Thanks!

One suggestion, though: given that the host argument is just yielded back to the caller, it seems a bit unnecessary. The name of this method also suggests that this method does something with it or uses it to determine what messages to yield.

Is there a reason you chose to pass it through? I'd probably (slightly) favor just putting a host = blah line at the call sites and then change this to each_message.

This comment has been minimized.

@JonRowe

JonRowe Aug 13, 2013

Member

I liked the look of passing it in and out rather than having a local variable, that was all

@JonRowe

JonRowe Aug 13, 2013

Member

I liked the look of passing it in and out rather than having a local variable, that was all

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Aug 13, 2013

Member

Whats the story with #401 and #404 and this?

Member

JonRowe commented Aug 13, 2013

Whats the story with #401 and #404 and this?

@xaviershay

This comment has been minimized.

Show comment
Hide comment
@xaviershay

xaviershay Aug 14, 2013

Member

#401 is broken, if it needs to be re-implemented this patch will have to change too. I need to understand how this PR interacts with it. Didn't leave myself enough time to do that tonight, unfortunately.

Member

xaviershay commented Aug 14, 2013

#401 is broken, if it needs to be re-implemented this patch will have to change too. I need to understand how this PR interacts with it. Didn't leave myself enough time to do that tonight, unfortunately.

# example method. They do not stash or restore existing method
# `add_stub` / `add_expectation` where it is known in advance that this
# is all that will be required of a stub, such as when passing attributes
# to the `double` example method. They do not stash or restore existing method
# definitions.

This comment has been minimized.

@myronmarston

myronmarston Aug 16, 2013

Member

As documented here, simple stubs/expectations do not stash or restore existing method defs. This is OK for test doubles (the original case @xaviershay had in mind for simple stubs) but not for partial mocks (as will be used with this feature). Can you add some specs (which should fail) about the resetting on a partial mocks, and then fix it? I suggested one possible fix here:

In #399 we'd like to use the new simple stub stuff, but I guess a simple stub on a partial mock is a "slightly less simple stub" since it needs to be reset properly. I think that should be taken care of as part of that ticket since it's not needed with what we currently have. Here's an idea for how to do that: In TestDouble#__build_mock_proxy it will instantiate a subclass of Proxy that overrides add_simple_stub to pass a support_reset: false flag to add_simple_stub. For other proxies (e.g. for a partial mock) it'll pass true for this flag, and based on the flag it'll either do configure_method or define_proxy_method; @needs_restoration = false. I think that'll work.

@myronmarston

myronmarston Aug 16, 2013

Member

As documented here, simple stubs/expectations do not stash or restore existing method defs. This is OK for test doubles (the original case @xaviershay had in mind for simple stubs) but not for partial mocks (as will be used with this feature). Can you add some specs (which should fail) about the resetting on a partial mocks, and then fix it? I suggested one possible fix here:

In #399 we'd like to use the new simple stub stuff, but I guess a simple stub on a partial mock is a "slightly less simple stub" since it needs to be reset properly. I think that should be taken care of as part of that ticket since it's not needed with what we currently have. Here's an idea for how to do that: In TestDouble#__build_mock_proxy it will instantiate a subclass of Proxy that overrides add_simple_stub to pass a support_reset: false flag to add_simple_stub. For other proxies (e.g. for a partial mock) it'll pass true for this flag, and based on the flag it'll either do configure_method or define_proxy_method; @needs_restoration = false. I think that'll work.

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Aug 19, 2013

Member

@myronmarston I've added in specs covering what happens when you attempt to use the negative (I'm disallowing it) but I'm having trouble consistently getting a partial mocking failure. What should trigger it? I don't want to create order dependant specs...

Member

JonRowe commented Aug 19, 2013

@myronmarston I've added in specs covering what happens when you attempt to use the negative (I'm disallowing it) but I'm having trouble consistently getting a partial mocking failure. What should trigger it? I don't want to create order dependant specs...

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 3, 2013

Member

Ping! Any advice?

Member

JonRowe commented Sep 3, 2013

Ping! Any advice?

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 9, 2013

Member

I'll squash this before we merge it but I'm leaving the history until I'm ready for that :)

Member

JonRowe commented Sep 9, 2013

I'll squash this before we merge it but I'm leaving the history until I'm ready for that :)

@coveralls

This comment has been minimized.

Show comment
Hide comment
@coveralls

coveralls Sep 9, 2013

Coverage Status

Coverage decreased (-0.06%) when pulling c3bdeff on receive_messages into 3ac6f4e on master.

coveralls commented Sep 9, 2013

Coverage Status

Coverage decreased (-0.06%) when pulling c3bdeff on receive_messages into 3ac6f4e on master.

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 9, 2013

Member

Ok, this now resets partial mocks properly (it was actually a fairly trivial change as we have a separate PartialMockProxy to handle this).

One thing I haven't done, and this is deliberate cause my head hurts just thinking about it, is handle resetting 'any_instance' stubs/expectations, because they are already reset entirely separately from everything else, and I'm using the existing stub/expectation functionality for those (it's not new code).

They already don't reset cleanly, so I figure that's a new issue.

Member

JonRowe commented Sep 9, 2013

Ok, this now resets partial mocks properly (it was actually a fairly trivial change as we have a separate PartialMockProxy to handle this).

One thing I haven't done, and this is deliberate cause my head hurts just thinking about it, is handle resetting 'any_instance' stubs/expectations, because they are already reset entirely separately from everything else, and I'm using the existing stub/expectation functionality for those (it's not new code).

They already don't reset cleanly, so I figure that's a new issue.

@myronmarston

This comment has been minimized.

Show comment
Hide comment
@myronmarston

myronmarston Sep 10, 2013

Member

They already don't reset cleanly, so I figure that's a new issue.

That's news to me. Can you open an rspec-mocks issue includes a code snippet that demonstrates the problem?

Member

myronmarston commented Sep 10, 2013

They already don't reset cleanly, so I figure that's a new issue.

That's news to me. Can you open an rspec-mocks issue includes a code snippet that demonstrates the problem?

myronmarston added a commit that referenced this pull request Sep 12, 2013

Merge pull request #399 from rspec/receive_messages
Allow multiple message allowances/expectations via `receive_messages`

@myronmarston myronmarston merged commit 69954e9 into master Sep 12, 2013

1 check passed

default The Travis CI build passed
Details

@myronmarston myronmarston deleted the receive_messages branch Sep 12, 2013

@myronmarston

This comment has been minimized.

Show comment
Hide comment
@myronmarston

myronmarston Sep 12, 2013

Member

Merged. Thanks @JonRowe!

Member

myronmarston commented Sep 12, 2013

Merged. Thanks @JonRowe!

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 12, 2013

Member

Weren't we going to squash this? :P

Member

JonRowe commented Sep 12, 2013

Weren't we going to squash this? :P

@myronmarston

This comment has been minimized.

Show comment
Hide comment
@myronmarston

myronmarston Sep 12, 2013

Member

Oh yeah.....too late now, I guess.

Member

myronmarston commented Sep 12, 2013

Oh yeah.....too late now, I guess.

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 12, 2013

Member

Yup, oh well :)

Member

JonRowe commented Sep 12, 2013

Yup, oh well :)

@rosenfeld

This comment has been minimized.

Show comment
Hide comment
@rosenfeld

rosenfeld commented Sep 12, 2013

👍

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