-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexception_hunter_spec.rb
212 lines (178 loc) · 5.64 KB
/
exception_hunter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
module ExceptionHunter
describe ::ExceptionHunter do
describe '.track' do
subject { ::ExceptionHunter.track(exception, custom_data: custom_data, user: user) }
let(:exception) do
RuntimeError.new('Some error').tap { |exception| exception.set_backtrace(caller) }
end
let(:custom_data) do
{
some_id: 12,
some_other_data: {
name: 'John'
}
}
end
let(:user) { OpenStruct.new(id: 3, email: 'example@example.com', name: 'John') }
context 'when tracking is enabled' do
before { allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(0) }
let(:error) { Error.last }
it 'creates a new error' do
expect { subject }.to change(Error, :count).by(1)
end
it 'tracks the exception data' do
subject
expect(error.class_name).to eq(exception.class.name)
expect(error.backtrace).to eq(exception.backtrace)
end
it 'tracks the custom data' do
subject
expect(error.custom_data).to eq({
'some_id' => 12,
'some_other_data' => {
'name' => 'John'
}
})
end
it 'tracks the user attributes' do
subject
expect(error.user_data).to eq({
'id' => 3,
'email' => 'example@example.com'
})
end
it 'adds the tag Manual to the error group' do
subject
expect(error.error_group.tags).to eq(['Manual'])
end
it ' does not create a new thread' do
expect(Thread).to_not receive(:new)
subject
end
context 'when the error is tracked within a transaction' do
before do
allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(1)
allow(Thread).to receive(:new).and_call_original
end
it 'creates a new error within a new thread' do
expect { subject }.to change(Error, :count).by(1)
expect(Thread).to have_received(:new)
end
end
end
context 'when tracking is disabled' do
before do
Config.enabled = false
end
after do
Config.enabled = true
end
it 'does not create a new error' do
expect { subject }.not_to change(Error, :count)
end
end
end
describe '.setup' do
let!(:original_queue_adapter) { ActiveJob::Base.queue_adapter }
let(:notifiers) { [notifier_1, notifier_2] }
let(:notifier_1) do
{
name: :slack,
options: {
webhook: 'test_webhook_1'
}
}
end
let(:notifier_2) do
{
name: :slack,
options: {
webhook: 'test_webhook_2'
}
}
end
before do
ActiveJob::Base.queue_adapter = :test
end
after do
ActiveJob::Base.queue_adapter = original_queue_adapter
end
subject do
ExceptionHunter.setup do |config|
config.notifiers = notifiers
end
end
context 'when notifiers config is correct' do
it 'does not raise an error' do
expect {
subject
}.not_to raise_error
end
end
context 'when notifiers config is not correct' do
context 'when wrong notifier name' do
let(:notifier_2) do
{
name: :slack_other,
options: {
webhook: 'test_webhook_2'
}
}
end
it 'raises an error' do
expect {
subject
}.to raise_error(ExceptionHunter::Notifiers::MisconfiguredNotifiers) { |error|
expect(error.message).to eq("Notifier has incorrect configuration: #{notifier_2.inspect}")
}
end
end
context 'when missing notifier webhook' do
let(:notifier_2) do
{
name: :slack,
options: {}
}
end
it 'raises an error' do
expect {
subject
}.to raise_error(ExceptionHunter::Notifiers::MisconfiguredNotifiers) { |error|
expect(error.message).to eq("Notifier has incorrect configuration: #{notifier_2.inspect}")
}
end
end
context 'when missing notifier options' do
let(:notifier_2) do
{
name: :slack
}
end
it 'raises an error' do
expect {
subject
}.to raise_error(ExceptionHunter::Notifiers::MisconfiguredNotifiers) { |error|
expect(error.message).to eq("Notifier has incorrect configuration: #{notifier_2.inspect}")
}
end
end
context 'when missing notifier name' do
let(:notifier_2) do
{
options: {
webhook: 'test_webhook_2'
}
}
end
it 'raises an error' do
expect {
subject
}.to raise_error(ExceptionHunter::Notifiers::MisconfiguredNotifiers) { |error|
expect(error.message).to eq("Notifier has incorrect configuration: #{notifier_2.inspect}")
}
end
end
end
end
end
end