-
-
Notifications
You must be signed in to change notification settings - Fork 356
Message expect/allow API independent of configurable syntax. #311
Conversation
This proposed API is problematic: RSpec::Mocks.allow_message(object, :message).with("foo").and_return(5) … because the A possible solution is this: # Simple case.
RSpec::Mocks.allow_message(object, :message)
# Using MessageExpectation methods like #with #and_return etc.
RSpec::Mocks.allow_message(object, :message) do |expectation|
expectation.with(:in).and_return(:out)
end
# Providing an implementation for the allowed/stubbed method.
RSpec::Mocks.allow_message(object, :message, ->{ :out })
# Providing implementation AND using MessageExpectation API.
# Not sure this is a real use-case…?
RSpec::Mocks.allow_message(object, :message, ->{ :out }) do |expectation|
# do something with expectation?
end I'm not sure I like this, and it's 2:22am so I'm going to bed. |
Thanks for taking a stab at this. I think you're right that if you just delegate to the def allow_message(subject, message, &block)
proxy = ::RSpec::Mocks.proxy_for(subject)
proxy.add_stub(caller(1)[0], message.to_sym, &block)
end
def expect_message(subject, message, &block)
proxy = ::RSpec::Mocks.proxy_for(subject)
proxy.add_message_expectation(caller(1)[0], message.to_sym, &block)
end Note that I haven't actually tested this. (And the |
Ah thanks, that's much better. I was looking too hard at the |
|
I believe this is now a working and tested version of |
Does this include exposing the any instance varieties? |
|
We could extend it to expose those, but, IMO, it's not needed.
I think
I like this idea.
Yep. I don't think we need a cuke for it, though; as I mentioned above, this is meant more for libraries to build off of than for end users, and the yard docs are sufficient for the purpose, IMO. |
I'm going to try and close this one out. |
Thanks @samphippen. I was thinking of doing it too, but got bogged down elsewhere yesterday evening. Let me know if I can help. |
@pda: this is looking really good, thanks for the contribution! @pda can you add some yard/rdoc for the methods you added as per @alindeman's suggestion.
@myronmarston: I think we should have a spec for this (that is negative expectations). @pda could you add one? If not I'll add one when I merge it in. I guess the spec would be something like "expect message works with never". Thanks! |
Also, though, if @pda is not able to get to these things, we can probably take it from here? Just let us know. |
Here's what I'm thinking for the yard docs: https://gist.github.com/samphippen/5892183. @myronmarston got opinions? |
Updated with some comments from @alindeman: https://gist.github.com/samphippen/75b8a6f6a8d3fdaeda89 |
I've finished this up for the old syntax. Can I get some review on: fcbbaba please. |
Ok, so all the tests aren't passing. Let me tinker. |
I guess this branch: https://github.com/rspec/rspec-mocks/compare/syntax_agnostic_message_matchers is actually the one that needs reviewing. If anyone could take a look and tell me what they think. It's not the most elegant solution, but the specs now at least all pass (locally). |
Signed-off-by: Sam Phippen <[email protected]>
@samphippen's wrap up of #311: syntax agnostic message matchers
See pull: #311 and #334. Signed-off-by: Sam Phippen <[email protected]> Conflicts: Changelog.md lib/rspec/mocks/message_expectation.rb spec/rspec/mocks/mock_spec.rb
See pull: #311 and #334. Signed-off-by: Sam Phippen <[email protected]> Conflicts: Changelog.md lib/rspec/mocks/message_expectation.rb spec/rspec/mocks/mock_spec.rb
Sorry to have vanished half way though this. |
See pull: rspec/rspec-mocks#311 and rspec/rspec-mocks#334. Signed-off-by: Sam Phippen <[email protected]> Conflicts: Changelog.md lib/rspec/mocks/message_expectation.rb spec/rspec/mocks/mock_spec.rb --- This commit was imported from rspec/rspec-mocks@0b2e5b2.
See pull: rspec/rspec-mocks#311 and rspec/rspec-mocks#334. Signed-off-by: Sam Phippen <[email protected]> Conflicts: Changelog.md lib/rspec/mocks/message_expectation.rb spec/rspec/mocks/mock_spec.rb --- This commit was imported from rspec/rspec-mocks@58488d8.
An API independent of configurable syntax for setting message expectations/allowances is necessary, as highlighted by rspec/rspec-rails#764 and #306.
Proposed interface by @myronmarston in rspec/rspec-rails#764:
I'll upgrade this to a pull request once I've written code at Rails Camp this weekend.