Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Mimic RSpec's exactly(x).times matcher #19

Merged
merged 3 commits into from

1 participant

@MDaubs
Collaborator

This adds the ability to use RSpec-like (exact) receive counts. Thanks @alenia

MDaubs and others added some commits
@MDaubs MDaubs Mimic rspec's exactly(x).times synax c565686
Daniela Wellisz and Nick Chaffee fixed bug with times called implementation that did not allow multipl…
…e assertions on different arguments
d86920d
@MDaubs MDaubs Merge pull request #1 from sharespost/d86920d265a4313bd3191d46102faef…
…1aabf1592

Can now assert against multiple method calls with different arguments
5f9b75f
@MDaubs
Collaborator

I should note that this breaks in RSpec 2.8. PR #23 suggests supporting only the three most recent versions of RSpec.

@MDaubs MDaubs merged commit f44afe9 into from
@MDaubs MDaubs deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 12, 2013
  1. @MDaubs
Commits on Feb 16, 2013
  1. fixed bug with times called implementation that did not allow multipl…

    Daniela Wellisz and Nick Chaffee authored
    …e assertions on different arguments
Commits on Feb 20, 2013
  1. @MDaubs

    Merge pull request #1 from sharespost/d86920d265a4313bd3191d46102faef…

    MDaubs authored
    …1aabf1592
    
    Can now assert against multiple method calls with different arguments
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 3 deletions.
  1. +21 −3 lib/rspec-spies.rb
  2. +21 −0 spec/rspec-spies_spec.rb
View
24 lib/rspec-spies.rb
@@ -33,21 +33,35 @@ def visibility_for_method
RSpec::Matchers.define :have_received do |method_name, args, block|
match do |actual|
- messages_received = actual.send(:__mock_proxy).instance_variable_get("@messages_received")
- messages_received.any? do |message|
+ @messages_received = actual.send(:__mock_proxy).instance_variable_get("@messages_received").dup
+ @messages_received.keep_if do |message|
received_method_name, received_args, received_block = *message
result = (received_method_name == method_name)
result &&= argument_expectation_class.new(@args || any_args).args_match?(received_args)
result &&= (received_block == block)
end
+
+ if @times
+ @messages_received.length == @expected_count
+ else
+ @messages_received.length > 0
+ end
end
chain :with do |*args|
@args = args
end
+ chain :exactly do |count|
+ @expected_count = count
+ end
+
+ chain :times do
+ @times = true
+ end
+
failure_message_for_should do |actual|
- "expected #{actual.inspect} to have received #{method_name.inspect}#{args_message}"
+ "expected #{actual.inspect} to have received #{method_name.inspect}#{args_message}#{times_message}"
end
failure_message_for_should_not do |actual|
@@ -61,4 +75,8 @@ def visibility_for_method
def args_message
@args ? " with #{@args.inspect}" : ""
end
+
+ def times_message
+ @times ? " exactly #{@expected_count} times but was called #{@messages_received.count} times" : ""
+ end
end
View
21 spec/rspec-spies_spec.rb
@@ -37,6 +37,27 @@ module Matchers
have_received(:slice).with(hash_including({ :foo => :baz })).matches?(@object).should be_false
end
+ it "matches if specifies exactly(x).times" do
+ @object.stub!(:slice)
+ @object.slice(5)
+ @object.slice(5)
+
+ have_received(:slice).exactly(1).times.matches?(@object).should be_false
+ have_received(:slice).exactly(2).times.matches?(@object).should be_true
+ have_received(:slice).exactly(3).times.matches?(@object).should be_false
+ end
+
+ it "matches if called multiple times with different arguments" do
+ @object.stub!(:slice)
+ @object.slice(1)
+ @object.slice(2)
+
+ have_received(:slice).with(1).matches?(@object).should be_true
+ have_received(:slice).with(2).matches?(@object).should be_true
+ have_received(:slice).with(3).matches?(@object).should be_false
+ end
+
+
it "does not match if method is called with incorrect args" do
@object.stub!(:slice)
@object.slice(3)
Something went wrong with that request. Please try again.