Skip to content

Commit

Permalink
Match yielded args with == or ===.
Browse files Browse the repository at this point in the history
Previously, this expectation failed:

  expect { |b| _yield_with_args(String, Fixnum, &b) }.to yield_with_args(String, Fixnum)

...because Class=== returns false when given itself.
  • Loading branch information
myronmarston committed Mar 30, 2012
1 parent eb9d327 commit 72c5dae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rspec/matchers/built_in/yield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def args_match?
unless match = all_args_match?
@args_failure = "yielded with unexpected arguments" +
"\nexpected: #{expected.inspect}" +
"\n got: #{actual.inspect} (compared using ===)"
"\n got: #{actual.inspect} (compared using === and ==)"
end

match
Expand All @@ -96,7 +96,7 @@ def all_args_match?
return false if @expected.size != @actual.size

@expected.zip(@actual).all? do |expected, actual|
expected === actual
expected === actual || actual == expected
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rspec/matchers/yield_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,20 @@ def _yield_with_args(*args)
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
end
end

describe "expect {...}.to yield_with_args(String, Fixnum)" do
it "passes if the block yields objects of the given types" do
expect { |b| _yield_with_args("string", 15, &b) }.to yield_with_args(String, Fixnum)
end

it "passes if the block yields the given types" do
expect { |b| _yield_with_args(String, Fixnum, &b) }.to yield_with_args(String, Fixnum)
end

it "fails if the block yields objects of different types" do
expect {
expect { |b| _yield_with_args(15, "string", &b) }.to yield_with_args(String, Fixnum)
}.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
end
end
end

0 comments on commit 72c5dae

Please sign in to comment.