@@ -6,232 +6,156 @@ class AnotherTestError < StandardError; end
66
77 describe "basic functionality" do
88 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
1410 end
1511
1612 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./ )
2116 end
2217
2318 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
2720 end
2821 end
2922
3023 describe "error class matching" do
3124 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 )
3726 end
3827
3928 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/ )
4634 end
4735 end
4836
4937 describe "error instance matching" do
5038 it "passes when error instance matches exactly" do
51- expected_error = TestError . new ( "exact message" )
52- test_block = proc do
39+ expect {
5340 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" ) )
5742 end
5843
5944 it "passes when error instance has empty expected message" do
60- expected_error = TestError . new ( "" )
61- test_block = proc do
45+ expect {
6246 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 ( "" ) )
6648 end
6749
6850 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'/ )
7656 end
7757 end
7858
7959 describe "regex pattern matching" do
8060 it "passes when error message matches pattern" do
81- test_block = proc do
61+ expect {
8262 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/ )
8664 end
8765
8866 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/ )
9572 end
9673 end
9774
9875 describe "failure messages for attribute mismatches" do
9976 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"}/ )
11082 end
11183
11284 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}/ )
12290 end
12391
12492 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/ )
13398 end
13499
135100 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"}/ )
145106 end
146107 end
147108
148109 describe "attribute matching with .with chain" do
149110 it "passes when attributes match exactly" do
150- test_block = proc do
111+ expect {
151112 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" )
155114 end
156115
157116 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 )
163122 end
164123
165124 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" ) )
171130 end
172131
173132 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"}/ )
180138 end
181139
182140 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-
205141 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./ )
216144 end
217145 end
218146
219147 describe "integration with actual usage patterns" do
220148 it "works with multiple error reports in a block" do
221- test_block = proc do
149+ expect {
222150 Rails . error . report ( StandardError . new ( "first error" ) )
223151 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 )
227153 end
228154
229155 it "works with matcher chaining" do
230- test_block = proc do
156+ expect {
231157 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
235159 end
236160 end
237161end
0 commit comments