File tree Expand file tree Collapse file tree 2 files changed +54
-4
lines changed
spec/rspec/rails/matchers Expand file tree Collapse file tree 2 files changed +54
-4
lines changed Original file line number Diff line number Diff line change @@ -32,10 +32,11 @@ def failure_message
32
32
message << "expected #{ actual . inspect } to be a new #{ expected . inspect } "
33
33
end
34
34
unless attributes_match? ( actual )
35
+ describe_unmatched_attributes = surface_descriptions_in ( unmatched_attributes )
35
36
if unmatched_attributes . size > 1
36
- message << "attributes #{ unmatched_attributes . inspect } were not set on #{ actual . inspect } "
37
+ message << "attributes #{ describe_unmatched_attributes . inspect } were not set on #{ actual . inspect } "
37
38
else
38
- message << "attribute #{ unmatched_attributes . inspect } was not set on #{ actual . inspect } "
39
+ message << "attribute #{ describe_unmatched_attributes . inspect } was not set on #{ actual . inspect } "
39
40
end
40
41
end
41
42
end . join ( ' and ' )
@@ -49,13 +50,13 @@ def attributes
49
50
50
51
def attributes_match? ( actual )
51
52
attributes . stringify_keys . all? do |key , value |
52
- actual . attributes [ key ] . eql? ( value )
53
+ values_match? ( value , actual . attributes [ key ] )
53
54
end
54
55
end
55
56
56
57
def unmatched_attributes
57
58
attributes . stringify_keys . reject do |key , value |
58
- actual . attributes [ key ] . eql? ( value )
59
+ values_match? ( value , actual . attributes [ key ] )
59
60
end
60
61
end
61
62
end
Original file line number Diff line number Diff line change @@ -65,6 +65,55 @@ def new_record?; true; end
65
65
end
66
66
end
67
67
68
+ context "with composable matchers" do
69
+ context "one attribute is a composable matcher" do
70
+ it "passes" do
71
+ expect ( record ) . to be_a_new ( record . class ) . with (
72
+ :foo => a_string_including ( "foo" ) )
73
+ end
74
+
75
+ it "fails" do
76
+ expect {
77
+ expect ( record ) . to be_a_new ( record . class ) . with (
78
+ :foo => a_string_matching ( "bar" ) )
79
+ } . to raise_error ( "attribute {\" foo\" =>(a string matching \" bar\" )} was not set on #{ record . inspect } " )
80
+ end
81
+
82
+ context "matcher is wrong type" do
83
+ it "fails" do
84
+ expect {
85
+ expect ( record ) . to be_a_new ( record . class ) . with (
86
+ :foo => a_hash_including ( { :no_foo => "foo" } ) )
87
+ } . to raise_error { |e |
88
+ expect ( e . message ) . to eq ( "no implicit conversion of Hash into String" ) . or eq ( "can't convert Hash into String" )
89
+ }
90
+ end
91
+ end
92
+ end
93
+
94
+ context "two attributes are composable matchers" do
95
+ context "both matchers present in actual" do
96
+ it "passes" do
97
+ expect ( record ) . to be_a_new ( record . class ) . with (
98
+ :foo => a_string_matching ( "foo" ) ,
99
+ :bar => a_string_matching ( "bar" )
100
+ )
101
+ end
102
+ end
103
+
104
+ context "only one matcher present in actual" do
105
+ it "fails" do
106
+ expect {
107
+ expect ( record ) . to be_a_new ( record . class ) . with (
108
+ :foo => a_string_matching ( "foo" ) ,
109
+ :bar => a_string_matching ( "barn" )
110
+ )
111
+ } . to raise_error ( "attribute {\" bar\" =>(a string matching \" barn\" )} was not set on #{ record . inspect } " )
112
+ end
113
+ end
114
+ end
115
+ end
116
+
68
117
context "no attributes same" do
69
118
it "fails" do
70
119
expect {
You can’t perform that action at this time.
0 commit comments