Skip to content

Commit

Permalink
Improve specs for a_block matcher and handle more edgecases
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Sep 17, 2018
1 parent 524637d commit 034a81b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/rspec/mocks/argument_list_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def initialize(*expected_args)
# @see #initialize
def args_match?(*args, &block)
expected_args = resolve_expected_args_based_on(args)
if [ArgumentMatchers::BlockMatcher::INSTANCE] == expected_args
ArgumentMatchers::BlockMatcher::INSTANCE === block
if ArgumentMatchers::BlockMatcher::INSTANCE == expected_args.last
Support::FuzzyMatcher.values_match?(expected_args, args + [block])
else
Support::FuzzyMatcher.values_match?(expected_args, args)
end
Expand Down
30 changes: 25 additions & 5 deletions spec/rspec/mocks/argument_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,34 @@ def ==(other)
end

describe "a_block" do
before { expect(a_double).to receive(:random_call).with(a_block) }

it "matches a method call with a block" do
a_double.random_call { :a_dummy_block }
expect(a_double).to receive(:msg).with(a_block)
a_double.msg { :a_dummy_block }
end

it "matches a method call with arguments and a block" do
expect(a_double).to receive(:msg).with(1, 2, anything, a_block)
a_double.msg(1,2,3) { :and_a_block }
end

it "matches a method call with a block and still allows and_yield" do
expect(a_double).to receive(:msg).with(a_block).and_yield(1)
a_double.msg { |a| expect(a).to eq(1) }
end

it "fails if the method call has no block", :reset => true do
expect(a_double).to receive(:msg).with(a_block)
expect { a_double.msg }.to fail_including "expected: (a block)"
end

it "fails if the method call omits a block when matching arguments", :reset => true do
expect(a_double).to receive(:msg).with(1, 2, anything, a_block)
expect { a_double.msg(1,2,3) }.to fail_including "expected: (1, 2, anything, a block)"
end

pending "fails if the method call has no block" do
expect { a_double.random_call }.to fail_including "(a block)"
it "fails if supplying an incorrect arity block when using and_yield", :reset => true do
expect(a_double).to receive(:msg).with(a_block).and_yield(1)
expect { a_double.msg { :a_bad_block } }.to fail_including " yielded |1| to block with"
end
end
end
Expand Down

0 comments on commit 034a81b

Please sign in to comment.