44 class TestError < StandardError ; end
55 class AnotherTestError < StandardError ; end
66
7- let ( :mock_error_reporter ) { double ( "ErrorReporter" ) }
8- let ( :subscribers ) { [ ] }
9-
10- before do
11- allow ( Rails ) . to receive ( :error ) . and_return ( mock_error_reporter )
12-
13- allow ( mock_error_reporter ) . to receive ( :subscribe ) do |subscriber |
14- subscribers << subscriber
15- end
16-
17- allow ( mock_error_reporter ) . to receive ( :unsubscribe ) do |subscriber |
18- subscribers . delete ( subscriber )
19- end
20-
21- allow ( mock_error_reporter ) . to receive ( :report ) do |error , **attrs |
22- subscribers . each { |subscriber | subscriber . report ( error , **attrs ) }
23- end
24- end
25-
26- after do
27- subscribers . clear
28- end
29-
307 describe "basic functionality" do
318 it "passes when an error is reported" do
329 test_block = proc do
@@ -118,53 +95,34 @@ class AnotherTestError < StandardError; end
11895 end
11996 end
12097
121- describe "symbol error matching" do
122- it "passes when symbol matches" do
123- test_block = proc do
124- Rails . error . report ( :test_symbol )
125- end
126-
127- expect ( test_block ) . to have_reported_error ( :test_symbol )
128- end
129-
130- it "fails when symbol does not match" do
131- test_block = proc do
132- Rails . error . report ( :actual_symbol )
133- end
134- matcher = have_reported_error ( :expected_symbol )
135-
136- expect ( matcher . matches? ( test_block ) ) . to be false
137- end
138- end
139-
14098 describe "attribute matching with .with chain" do
14199 it "passes when attributes match exactly" do
142100 test_block = proc do
143- Rails . error . report ( StandardError . new ( "test" ) , user_id : 123 , context : "test" )
101+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "test" } )
144102 end
145103
146104 expect ( test_block ) . to have_reported_error . with ( user_id : 123 , context : "test" )
147105 end
148106
149107 it "passes with partial attribute matching" do
150108 test_block = proc do
151- Rails . error . report ( StandardError . new ( "test" ) , user_id : 123 , context : "test" , extra : "data" )
109+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "test" , extra : "data" } )
152110 end
153111
154112 expect ( test_block ) . to have_reported_error . with ( user_id : 123 )
155113 end
156114
157115 it "passes with hash matching using RSpec matchers" do
158116 test_block = proc do
159- Rails . error . report ( StandardError . new ( "test" ) , params : { foo : "bar" , baz : "qux" } )
117+ Rails . error . report ( StandardError . new ( "test" ) , context : { params : { foo : "bar" , baz : "qux" } } )
160118 end
161119
162120 expect ( test_block ) . to have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
163121 end
164122
165123 it "fails when attributes do not match" do
166124 test_block = proc do
167- Rails . error . report ( StandardError . new ( "test" ) , user_id : 123 , context : "actual" )
125+ Rails . error . report ( StandardError . new ( "test" ) , context : { user_id : 123 , context : "actual" } )
168126 end
169127 matcher = have_reported_error . with ( user_id : 456 , context : "expected" )
170128
@@ -185,8 +143,6 @@ class AnotherTestError < StandardError; end
185143 Rails . error . report ( StandardError . new ( "test" ) )
186144 end
187145
188- expect ( mock_error_reporter ) . to receive ( :unsubscribe )
189-
190146 expect ( test_block ) . to have_reported_error
191147 end
192148
@@ -196,8 +152,6 @@ class AnotherTestError < StandardError; end
196152 raise "unexpected error"
197153 end
198154
199- expect ( mock_error_reporter ) . to receive ( :unsubscribe )
200-
201155 expect {
202156 have_reported_error . matches? ( test_block )
203157 } . to raise_error ( "unexpected error" )
@@ -224,7 +178,7 @@ class AnotherTestError < StandardError; end
224178
225179 it "works with matcher chaining" do
226180 test_block = proc do
227- Rails . error . report ( TestError . new ( "test" ) , user_id : 123 )
181+ Rails . error . report ( TestError . new ( "test" ) , context : { user_id : 123 } )
228182 end
229183
230184 expect ( test_block ) . to have_reported_error ( TestError ) . and have_reported_error
0 commit comments