@@ -6,232 +6,156 @@ class AnotherTestError < StandardError; end
6
6
7
7
describe "basic functionality" do
8
8
it "passes when an error is reported" do
9
- test_block = proc do
10
- Rails . error . report ( StandardError . new ( "test error" ) )
11
- end
12
-
13
- expect ( test_block ) . to have_reported_error
9
+ expect { Rails . error . report ( StandardError . new ( "test error" ) ) } . to have_reported_error
14
10
end
15
11
16
12
it "fails when no error is reported" do
17
- test_block = proc { "no error" }
18
- matcher = have_reported_error
19
-
20
- expect ( matcher . matches? ( test_block ) ) . to be false
13
+ expect {
14
+ expect { "no error" } . to have_reported_error
15
+ } . to fail_with ( /Expected the block to report an error, but none was reported./ )
21
16
end
22
17
23
18
it "passes when negated and no error is reported" do
24
- test_block = proc { "no error" }
25
-
26
- expect ( test_block ) . not_to have_reported_error
19
+ expect { "no error" } . not_to have_reported_error
27
20
end
28
21
end
29
22
30
23
describe "error class matching" do
31
24
it "passes when correct error class is reported" do
32
- test_block = proc do
33
- Rails . error . report ( TestError . new ( "test error" ) )
34
- end
35
-
36
- expect ( test_block ) . to have_reported_error ( TestError )
25
+ expect { Rails . error . report ( TestError . new ( "test error" ) ) } . to have_reported_error ( TestError )
37
26
end
38
27
39
28
it "fails when wrong error class is reported" do
40
- test_block = proc do
41
- Rails . error . report ( AnotherTestError . new ( "wrong error" ) )
42
- end
43
- matcher = have_reported_error ( TestError )
44
-
45
- expect ( matcher . matches? ( test_block ) ) . to be false
29
+ expect {
30
+ expect {
31
+ Rails . error . report ( AnotherTestError . new ( "wrong error" ) )
32
+ } . to have_reported_error ( TestError )
33
+ } . to fail_with ( /Expected error to be an instance of TestError, but got AnotherTestError/ )
46
34
end
47
35
end
48
36
49
37
describe "error instance matching" do
50
38
it "passes when error instance matches exactly" do
51
- expected_error = TestError . new ( "exact message" )
52
- test_block = proc do
39
+ expect {
53
40
Rails . error . report ( TestError . new ( "exact message" ) )
54
- end
55
-
56
- expect ( test_block ) . to have_reported_error ( expected_error )
41
+ } . to have_reported_error ( TestError . new ( "exact message" ) )
57
42
end
58
43
59
44
it "passes when error instance has empty expected message" do
60
- expected_error = TestError . new ( "" )
61
- test_block = proc do
45
+ expect {
62
46
Rails . error . report ( TestError . new ( "any message" ) )
63
- end
64
-
65
- expect ( test_block ) . to have_reported_error ( expected_error )
47
+ } . to have_reported_error ( TestError . new ( "" ) )
66
48
end
67
49
68
50
it "fails when error instance has different message" do
69
- expected_error = TestError . new ( "expected message" )
70
- test_block = proc do
71
- Rails . error . report ( TestError . new ( "actual message" ) )
72
- end
73
- matcher = have_reported_error ( expected_error )
74
-
75
- expect ( matcher . matches? ( test_block ) ) . to be false
51
+ expect {
52
+ expect {
53
+ Rails . error . report ( TestError . new ( "actual message" ) )
54
+ } . to have_reported_error ( TestError . new ( "expected message" ) )
55
+ } . to fail_with ( /Expected error to be TestError with message 'expected message', but got TestError with message: 'actual message'/ )
76
56
end
77
57
end
78
58
79
59
describe "regex pattern matching" do
80
60
it "passes when error message matches pattern" do
81
- test_block = proc do
61
+ expect {
82
62
Rails . error . report ( StandardError . new ( "error with pattern" ) )
83
- end
84
-
85
- expect ( test_block ) . to have_reported_error ( /with pattern/ )
63
+ } . to have_reported_error ( /with pattern/ )
86
64
end
87
65
88
66
it "fails when error message does not match pattern" do
89
- test_block = proc do
90
- Rails . error . report ( StandardError . new ( "error without match" ) )
91
- end
92
- matcher = have_reported_error ( /different pattern/ )
93
-
94
- expect ( matcher . matches? ( test_block ) ) . to be false
67
+ expect {
68
+ expect {
69
+ Rails . error . report ( StandardError . new ( "error without match" ) )
70
+ } . to have_reported_error ( /different pattern/ )
71
+ } . to fail_with ( /Expected error message to match/ )
95
72
end
96
73
end
97
74
98
75
describe "failure messages for attribute mismatches" do
99
76
it "provides detailed failure message when attributes don't match" do
100
- test_block = proc do
101
- Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
102
- end
103
- matcher = have_reported_error . with ( user_id : 456 , context : "expected" )
104
-
105
- matcher . matches? ( test_block )
106
-
107
- expect ( matcher . failure_message ) . to include ( "Expected error attributes to match" )
108
- expect ( matcher . failure_message ) . to include ( "user_id: 456" )
109
- expect ( matcher . failure_message ) . to include ( "context: \" expected\" " )
77
+ expect {
78
+ expect {
79
+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
80
+ } . to have_reported_error . with ( user_id : 456 , context : "expected" )
81
+ } . to fail_with ( /Expected error attributes to match {user_id: 456, context: "expected"}, but got these mismatches: {user_id: 456, context: "expected"} and actual values are {"user_id" => 123, "context" => "actual"}/ )
110
82
end
111
83
112
84
it "identifies partial attribute mismatches correctly" do
113
- test_block = proc do
114
- Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , status : "active" , role : "admin" } )
115
- end
116
- matcher = have_reported_error . with ( user_id : 456 , status : "active" ) # user_id wrong, status correct
117
-
118
- matcher . matches? ( test_block )
119
-
120
- failure_msg = matcher . failure_message
121
- expect ( failure_msg ) . to include ( "got these mismatches: {user_id: 456}" )
85
+ expect {
86
+ expect {
87
+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , status : "active" , role : "admin" } )
88
+ } . to have_reported_error . with ( user_id : 456 , status : "active" ) # user_id wrong, status correct
89
+ } . to fail_with ( /got these mismatches: {user_id: 456}/ )
122
90
end
123
91
124
92
it "handles RSpec matcher mismatches in failure messages" do
125
- test_block = proc do
126
- Rails . error . report ( StandardError . new ( "test" ) , context : { params : { foo : "different" } } )
127
- end
128
- matcher = have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
129
-
130
- matcher . matches? ( test_block )
131
-
132
- expect ( matcher . failure_message ) . to include ( "Expected error attributes to match" )
93
+ expect {
94
+ expect {
95
+ Rails . error . report ( StandardError . new ( "test" ) , context : { params : { foo : "different" } } )
96
+ } . to have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
97
+ } . to fail_with ( /Expected error attributes to match/ )
133
98
end
134
99
135
100
it "shows actual context values when attributes don't match" do
136
- test_block = proc do
137
- Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
138
- end
139
- matcher = have_reported_error . with ( user_id : 456 )
140
-
141
- matcher . matches? ( test_block )
142
-
143
- failure_msg = matcher . failure_message
144
- expect ( failure_msg ) . to include ( "actual values are {\" user_id\" => 123, \" context\" => \" actual\" }" )
101
+ expect {
102
+ expect {
103
+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
104
+ } . to have_reported_error . with ( user_id : 456 )
105
+ } . to fail_with ( /actual values are {"user_id" => 123, "context" => "actual"}/ )
145
106
end
146
107
end
147
108
148
109
describe "attribute matching with .with chain" do
149
110
it "passes when attributes match exactly" do
150
- test_block = proc do
111
+ expect {
151
112
Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "test" } )
152
- end
153
-
154
- expect ( test_block ) . to have_reported_error . with ( user_id : 123 , context : "test" )
113
+ } . to have_reported_error . with ( user_id : 123 , context : "test" )
155
114
end
156
115
157
116
it "passes with partial attribute matching" do
158
- test_block = proc do
159
- Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "test" , extra : "data" } )
160
- end
161
-
162
- expect ( test_block ) . to have_reported_error . with ( user_id : 123 )
117
+ expect {
118
+ Rails . error . report (
119
+ StandardError . new ( "test" ) , context : { user_id : 123 , context : "test" , extra : "data" }
120
+ )
121
+ } . to have_reported_error . with ( user_id : 123 )
163
122
end
164
123
165
124
it "passes with hash matching using RSpec matchers" do
166
- test_block = proc do
167
- Rails . error . report ( StandardError . new ( "test" ) , context : { params : { foo : "bar" , baz : "qux" } } )
168
- end
169
-
170
- expect ( test_block ) . to have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
125
+ expect {
126
+ Rails . error . report (
127
+ StandardError . new ( "test" ) , context : { params : { foo : "bar" , baz : "qux" } }
128
+ )
129
+ } . to have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
171
130
end
172
131
173
132
it "fails when attributes do not match" do
174
- test_block = proc do
175
- Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
176
- end
177
- matcher = have_reported_error . with ( user_id : 456 , context : "expected" )
178
-
179
- expect ( matcher . matches? ( test_block ) ) . to be false
133
+ expect {
134
+ expect {
135
+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
136
+ } . to have_reported_error . with ( user_id : 456 , context : "expected" )
137
+ } . to fail_with ( /Expected error attributes to match {user_id: 456, context: "expected"}, but got these mismatches: {user_id: 456, context: "expected"} and actual values are {"user_id" => 123, "context" => "actual"}/ )
180
138
end
181
139
182
140
it "fails when no error is reported but attributes are expected" do
183
- test_block = proc { "no error" }
184
- matcher = have_reported_error . with ( user_id : 123 )
185
-
186
- expect ( matcher . matches? ( test_block ) ) . to be false
187
- end
188
- end
189
-
190
- describe "cleanup behavior" do
191
- it "unsubscribes from error reporter on successful completion" do
192
- test_block = proc do
193
- Rails . error . report ( StandardError . new ( "test" ) )
194
- end
195
-
196
- expect ( test_block ) . to have_reported_error
197
- end
198
-
199
- it "unsubscribes from error reporter even when exception is raised" do
200
- test_block = proc do
201
- Rails . error . report ( StandardError . new ( "test" ) )
202
- raise "unexpected error"
203
- end
204
-
205
141
expect {
206
- have_reported_error . matches? ( test_block )
207
- } . to raise_error ( "unexpected error" )
208
- end
209
- end
210
-
211
- describe "block expectations support" do
212
- it "declares support for block expectations" do
213
- matcher = have_reported_error
214
- expect ( matcher ) . to respond_to ( :supports_block_expectations? )
215
- expect ( matcher . supports_block_expectations? ) . to be true
142
+ expect { "no error" } . to have_reported_error . with ( user_id : 123 )
143
+ } . to fail_with ( /Expected the block to report an error, but none was reported./ )
216
144
end
217
145
end
218
146
219
147
describe "integration with actual usage patterns" do
220
148
it "works with multiple error reports in a block" do
221
- test_block = proc do
149
+ expect {
222
150
Rails . error . report ( StandardError . new ( "first error" ) )
223
151
Rails . error . report ( TestError . new ( "second error" ) )
224
- end
225
-
226
- expect ( test_block ) . to have_reported_error ( StandardError )
152
+ } . to have_reported_error ( StandardError )
227
153
end
228
154
229
155
it "works with matcher chaining" do
230
- test_block = proc do
156
+ expect {
231
157
Rails . error . report ( TestError . new ( "test" ) , context : { user_id : 123 } )
232
- end
233
-
234
- expect ( test_block ) . to have_reported_error ( TestError ) . and have_reported_error
158
+ } . to have_reported_error ( TestError ) . and have_reported_error
235
159
end
236
160
end
237
161
end
0 commit comments