Skip to content

Commit

Permalink
Can match against num times invoked with specific args
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Mar 10, 2012
1 parent 9c4ff58 commit 4efa891
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -5,3 +5,4 @@ TODO
* deal with default: [], probably by marshall dumping and loading
* need to be able to mimick a class
* need to be able to talk about nouns
* .with() <-- no args, what should error messages be?
22 changes: 20 additions & 2 deletions lib/mockingbird/rspec_matchers.rb
Expand Up @@ -44,6 +44,7 @@ def failure_message_for_should_not

match_num_times = Module.new do
attr_accessor :expected_times_invoked

def match?
expected_times_invoked == times_invoked
end
Expand All @@ -58,6 +59,23 @@ def failure_message_for_should_not
end


match_num_times_with = Module.new do
attr_accessor :expected_times_invoked, :expected_arguments

def match?
invocations.select { |invocation| invocation == expected_arguments }.size == expected_times_invoked
end

def failure_message_for_should
"should have been told to #{verb} #{times_msg expected_times_invoked} with `#{expected_arguments.map(&:inspect).join ', '}'"
end

def failure_message_for_should_not
failure_message_for_should.sub "should", "should not"
end
end


RSpec::Matchers.define :have_been_told_to do |verb|
use_case = handler.new verb

Expand All @@ -67,12 +85,12 @@ def failure_message_for_should_not
end

chain :times do |number|
use_case.extend match_num_times
use_case.extend (use_case.kind_of?(with_arguments) ? match_num_times_with : match_num_times)
use_case.expected_times_invoked = number
end

chain :with do |*arguments|
use_case.extend with_arguments
use_case.extend (use_case.kind_of?(match_num_times) ? match_num_times_with : with_arguments)
use_case.expected_arguments = arguments
end

Expand Down
33 changes: 33 additions & 0 deletions spec/rspec_matchers_spec.rb
Expand Up @@ -90,4 +90,37 @@
end
end
end


describe 'conjunction of with(args) and times(n)' do
before { mocked_class.sing :wink, default: nil }
let(:instance) { mocked_class.new }

example 'default use case' do
instance.should have_been_told_to(:wink).times(0).with(1, '2')
instance.should_not have_been_told_to(:wink).times(1).with(1, '2')
instance.wink
instance.should have_been_told_to(:wink).times(0).with(1, '2')
instance.should_not have_been_told_to(:wink).times(1).with(1, '2')
instance.wink 1, '2'
instance.should have_been_told_to(:wink).times(1).with(1, '2')
instance.wink 1, '2' # correct one
instance.wink 1, '3'
instance.wink 2, '2'
instance.wink 1, '2', 3
instance.should have_been_told_to(:wink).times(2).with(1, '2')
end

example 'failure message for should' do
expect { instance.should have_been_told_to(:wink).times(1).with(1, '2') }.to \
raise_error(RSpec::Expectations::ExpectationNotMetError, /should have been told to wink 1 time with `1, "2"'/)
end

example 'failure message for should not' do
instance.wink 1, '2'
instance.wink 1, '2'
expect { instance.should_not have_been_told_to(:wink).times(2).with(1, '2') }.to \
raise_error(RSpec::Expectations::ExpectationNotMetError, /should not have been told to wink 2 times with `1, "2"'/)
end
end
end

0 comments on commit 4efa891

Please sign in to comment.