File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed
lib/rspec/collection_matchers
spec/rspec/collection_matchers Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ module RSpec
2
2
module CollectionMatchers
3
3
class Have
4
4
QUERY_METHODS = [ :size , :length , :count ] . freeze
5
+ IGNORED_CLASSES = [ Integer ] . freeze
5
6
6
7
def initialize ( expected , relativity = :exactly )
7
8
@expected = case expected
@@ -33,7 +34,7 @@ def matches?(collection_or_owner)
33
34
raise not_a_collection if @actual . nil?
34
35
else
35
36
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 )
37
38
@actual = collection . __send__ ( query_method )
38
39
end
39
40
case @relativity
@@ -60,6 +61,10 @@ def determine_query_method(collection)
60
61
QUERY_METHODS . detect { |m | collection . respond_to? ( m ) }
61
62
end
62
63
64
+ def is_ignored_class? ( collection )
65
+ IGNORED_CLASSES . any? { |klass | klass === collection }
66
+ end
67
+
63
68
def not_a_collection
64
69
"expected #{ @collection_name } to be a collection but it does not respond to #length, #size or #count"
65
70
end
Original file line number Diff line number Diff line change @@ -105,6 +105,17 @@ def create_collection_owner_with(n)
105
105
end
106
106
107
107
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
+
108
119
it "provides a meaningful error" do
109
120
owner = Class . new do
110
121
def items
You can’t perform that action at this time.
0 commit comments