Skip to content
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

Fail Include matcher when comparing String with non-String #1449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/rspec/matchers/built_in/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def actual_hash_has_key?(expected_key)
end

def actual_collection_includes?(expected_item)
# if the actual is a String, the expected_item must also be a String
return false if actual.is_a?(String) && !expected_item.is_a?(String)
pirj marked this conversation as resolved.
Show resolved Hide resolved

return true if actual.include?(expected_item)

# String lacks an `any?` method...
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/matchers/built_in/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ def hash.send; :sent; end
expect("abc").to include("a")
end

it "passes if target includes multiple expecteds" do
expect(" foo\nbar\nbazz").to include("foo", "bar", "baz")
end

it "fails if target does not include expected" do
expect {
expect("abc").to include("d")
Expand All @@ -227,6 +231,12 @@ def hash.send; :sent; end
}.to fail_with(a_string_not_matching(/Diff/i))
end

it "fails when matching a collection" do
expect {
expect("foo").to include({:bar => "baz"})
}.to fail_with(/expected "foo" to include {:bar => "baz"}/)
end

context "with exact count" do
it 'fails if the block yields wrong number of times' do
expect {
Expand Down