4
4
class TestError < StandardError ; end
5
5
class AnotherTestError < StandardError ; end
6
6
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
-
30
7
describe "basic functionality" do
31
8
it "passes when an error is reported" do
32
9
test_block = proc do
@@ -118,53 +95,34 @@ class AnotherTestError < StandardError; end
118
95
end
119
96
end
120
97
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
-
140
98
describe "attribute matching with .with chain" do
141
99
it "passes when attributes match exactly" do
142
100
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" } )
144
102
end
145
103
146
104
expect ( test_block ) . to have_reported_error . with ( user_id : 123 , context : "test" )
147
105
end
148
106
149
107
it "passes with partial attribute matching" do
150
108
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" } )
152
110
end
153
111
154
112
expect ( test_block ) . to have_reported_error . with ( user_id : 123 )
155
113
end
156
114
157
115
it "passes with hash matching using RSpec matchers" do
158
116
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" } } )
160
118
end
161
119
162
120
expect ( test_block ) . to have_reported_error . with ( params : a_hash_including ( foo : "bar" ) )
163
121
end
164
122
165
123
it "fails when attributes do not match" do
166
124
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" } )
168
126
end
169
127
matcher = have_reported_error . with ( user_id : 456 , context : "expected" )
170
128
@@ -185,8 +143,6 @@ class AnotherTestError < StandardError; end
185
143
Rails . error . report ( StandardError . new ( "test" ) )
186
144
end
187
145
188
- expect ( mock_error_reporter ) . to receive ( :unsubscribe )
189
-
190
146
expect ( test_block ) . to have_reported_error
191
147
end
192
148
@@ -196,8 +152,6 @@ class AnotherTestError < StandardError; end
196
152
raise "unexpected error"
197
153
end
198
154
199
- expect ( mock_error_reporter ) . to receive ( :unsubscribe )
200
-
201
155
expect {
202
156
have_reported_error . matches? ( test_block )
203
157
} . to raise_error ( "unexpected error" )
@@ -224,7 +178,7 @@ class AnotherTestError < StandardError; end
224
178
225
179
it "works with matcher chaining" do
226
180
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 } )
228
182
end
229
183
230
184
expect ( test_block ) . to have_reported_error ( TestError ) . and have_reported_error
0 commit comments