File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed
lib/rspec/matchers/built_in
spec/rspec/matchers/built_in Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,17 @@ def with_captures(*captures)
35
35
self
36
36
end
37
37
38
+ # @api private
39
+ # @return [String]
40
+ def failure_message
41
+ if Array === expected && !( actual . respond_to? ( :to_a ) || actual . respond_to? ( :to_ary ) )
42
+ return "expected a collection that can be converted to an array with " \
43
+ "`#to_ary` or `#to_a`, but got #{ actual_formatted } "
44
+ end
45
+
46
+ super
47
+ end
48
+
38
49
private
39
50
40
51
def match ( expected , actual )
@@ -46,6 +57,7 @@ def match(expected, actual)
46
57
47
58
def can_safely_call_match? ( expected , actual )
48
59
return false unless actual . respond_to? ( :match )
60
+ return false if Array === expected
49
61
50
62
!( RSpec ::Matchers . is_a_matcher? ( expected ) &&
51
63
( String === actual || Regexp === actual ) )
Original file line number Diff line number Diff line change 95
95
expect ( description ) . to eq ( "match [(a string matching /foo/), (a value within 0.2 of 1)]" )
96
96
end
97
97
end
98
+
99
+ it "fails when target type (String) does not match expected (Array)" do
100
+ expect {
101
+ expect ( "string" ) . to match ( [ "c" , "a" , "b" ] )
102
+ } . to fail_with ( 'expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got "string"' )
103
+ end
98
104
end
99
105
100
106
RSpec . describe "expect(...).not_to match(expected)" do
135
141
} . to fail_with ( 'expected ["fod", 1.1] not to match [(a string matching /fod/), (a value within 0.2 of 1)]' )
136
142
end
137
143
end
144
+
145
+ it "passes when target type (String) does not match expected (Array)" do
146
+ expect ( "string" ) . not_to match ( [ "c" , "a" , "b" ] )
147
+ end
138
148
end
You can’t perform that action at this time.
0 commit comments