Skip to content

Commit 9208af8

Browse files
committed
Merge pull request #13 from kassio/master
Adds a blacklist of classes to be ignored
2 parents 03b3a3c + f63d92c commit 9208af8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/rspec/collection_matchers/have.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module RSpec
22
module CollectionMatchers
33
class Have
44
QUERY_METHODS = [:size, :length, :count].freeze
5+
IGNORED_CLASSES = [Integer].freeze
56

67
def initialize(expected, relativity=:exactly)
78
@expected = case expected
@@ -33,7 +34,7 @@ def matches?(collection_or_owner)
3334
raise not_a_collection if @actual.nil?
3435
else
3536
query_method = determine_query_method(collection)
36-
raise not_a_collection unless query_method
37+
raise not_a_collection if !query_method || is_ignored_class?(collection)
3738
@actual = collection.__send__(query_method)
3839
end
3940
case @relativity
@@ -60,6 +61,10 @@ def determine_query_method(collection)
6061
QUERY_METHODS.detect {|m| collection.respond_to?(m)}
6162
end
6263

64+
def is_ignored_class?(collection)
65+
IGNORED_CLASSES.any? {|klass| klass === collection}
66+
end
67+
6368
def not_a_collection
6469
"expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
6570
end

spec/rspec/collection_matchers/have_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ def create_collection_owner_with(n)
105105
end
106106

107107
describe "expect(...).to have(n).items where result responds to items but returns something other than a collection" do
108+
it "provides a meaningful error" do
109+
owner = Class.new do
110+
def items
111+
1
112+
end
113+
end.new
114+
expect do
115+
expect(owner).to have(3).items
116+
end.to raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
117+
end
118+
108119
it "provides a meaningful error" do
109120
owner = Class.new do
110121
def items

0 commit comments

Comments
 (0)