-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
Verify the ordering of spies. #439
Changes from all commits
b727d67
59a95de
9dcac41
056d2fc
939236f
0ada1b9
9c5a718
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ def initialize(error_generator, expectation_ordering, expected_from, method_doub | |
@expected_received_count = expected_received_count | ||
@argument_list_matcher = ArgumentListMatcher::MATCH_ALL | ||
@order_group = expectation_ordering | ||
@order_group.register(self) | ||
@ordered = false | ||
@at_least = @at_most = @exactly = nil | ||
@args_to_yield = [] | ||
@failed_fast = nil | ||
|
@@ -277,6 +279,11 @@ def expected_messages_received? | |
ignoring_args? || matches_exact_count? || matches_at_least_count? || matches_at_most_count? | ||
end | ||
|
||
def ensure_expected_ordering_received! | ||
@order_group.verify_invocation_order(self) if @ordered | ||
true | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully this is clearer @myronmarston? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much clearer :). It's unclear to me why you are returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because:
is much nicer than:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I.e. at some point this has to pretend it fits into the predicate pattern, because of how the matcher is implemented. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd rather the matcher be implemented like so: return false if @expectation.expected_messages_received?
@expectation.ensure_expected_ordering_received!
true I'm big on command/query separation, though, so I like pushing it through. YMMV :). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm big on command/query separation, but it's just pretending here (because its starting as a query) so I prefer the elimination of conditionals here. |
||
|
||
# @private | ||
def ignoring_args? | ||
@expected_received_count == :any | ||
|
@@ -462,11 +469,15 @@ def twice(&block) | |
# api.should_receive(:finish).ordered | ||
def ordered(&block) | ||
self.inner_implementation_action = block | ||
@order_group.register(self) | ||
@ordered = true | ||
self | ||
end | ||
|
||
# @private | ||
def ordered? | ||
@ordered | ||
end | ||
|
||
# @private | ||
def negative_expectation_for?(message) | ||
@message == message && negative? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
describe 'OrderGroup' do | ||
let(:order_group) { ::RSpec::Mocks::OrderGroup.new } | ||
|
||
describe '#consume' do | ||
let(:ordered_1) { double :ordered? => true } | ||
let(:ordered_2) { double :ordered? => true } | ||
let(:unordered) { double :ordered? => false } | ||
|
||
before do | ||
order_group.register unordered | ||
order_group.register ordered_1 | ||
order_group.register unordered | ||
order_group.register ordered_2 | ||
order_group.register unordered | ||
order_group.register unordered | ||
end | ||
|
||
it 'returns the first ordered? expectation' do | ||
expect(order_group.consume).to eq ordered_1 | ||
end | ||
it 'keeps returning ordered? expectation until all are returned' do | ||
expectations = 3.times.map { order_group.consume } | ||
expect(expectations).to eq [ordered_1, ordered_2, nil] | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it could, hang over from initially building an until empty loop, but it broke when it failed ;) |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a little confusing for this method to be called
expected_messages_received?
when it does more than that now. Maybe it should be renamed to something likeexpected_messages_received_in_specified_order?
or something?