/
have_enqueued_mail_spec.rb
417 lines (346 loc) · 14.6 KB
/
have_enqueued_mail_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
require "rspec/rails/feature_check"
if RSpec::Rails::FeatureCheck.has_active_job?
require "action_mailer"
require "rspec/rails/matchers/have_enqueued_mail"
class TestMailer < ActionMailer::Base
def test_email; end
def email_with_args(arg1, arg2); end
def email_with_optional_args(required_arg, optional_arg = nil); end
end
class AnotherTestMailer < ActionMailer::Base
def test_email; end
end
if RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery?
class UnifiedMailer < ActionMailer::Base
self.delivery_job = ActionMailer::MailDeliveryJob
def test_email; end
def email_with_args(arg1, arg2); end
end
class DeliveryJobSubClass < ActionMailer::DeliveryJob
end
class UnifiedMailerWithDeliveryJobSubClass < ActionMailer::Base
self.delivery_job = DeliveryJobSubClass
def test_email; end
end
end
end
RSpec.describe "HaveEnqueuedMail matchers", skip: !RSpec::Rails::FeatureCheck.has_active_job? do
before do
ActiveJob::Base.queue_adapter = :test
end
around do |example|
original_logger = ActiveJob::Base.logger
ActiveJob::Base.logger = Logger.new(nil) # Silence messages "[ActiveJob] Enqueued ...".
example.run
ActiveJob::Base.logger = original_logger
end
describe "have_enqueued_mail" do
it "passes when a mailer method is called with deliver_later" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email)
end
it "passes when using the have_enqueued_email alias" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_email(TestMailer, :test_email)
end
it "passes when using the enqueue_mail alias" do
expect {
TestMailer.test_email.deliver_later
}.to enqueue_mail(TestMailer, :test_email)
end
it "passes when using the enqueue_email alias" do
expect {
TestMailer.test_email.deliver_later
}.to enqueue_email(TestMailer, :test_email)
end
it "passes when negated" do
expect { }.not_to have_enqueued_mail(TestMailer, :test_email)
end
it "passes when given 0 arguments" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_email
end
it "passes when negated with 0 arguments" do
expect { }.not_to have_enqueued_email
end
it "passes when only given mailer argument" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_email(TestMailer)
end
it "passes when negated with only mailer arguments" do
expect { }.not_to have_enqueued_email(TestMailer)
end
it "ensure that the right mailer is enqueued" do
expect {
expect {
AnotherTestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer)
}.to raise_error(/expected to enqueue TestMailer exactly 1 time but enqueued 0/)
end
it "counts only emails enqueued in the block" do
TestMailer.test_email.deliver_later
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).once
end
it "fails when too many emails are enqueued" do
expect {
expect {
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).exactly(1)
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time/)
end
it "matches based on mailer class and method name" do
expect {
TestMailer.test_email.deliver_later
TestMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).once
end
it "passes with multiple emails" do
expect {
TestMailer.test_email.deliver_later
TestMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).and have_enqueued_mail(TestMailer, :email_with_args)
end
it 'fails when negated and mail is enqueued' do
expect {
expect {
TestMailer.test_email.deliver_later
}.not_to have_enqueued_mail(TestMailer, :test_email)
}.to raise_error(/expected not to enqueue TestMailer.test_email at least 1 time but enqueued 1/)
end
it "passes with :once count" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).once
end
it "passes with :twice count" do
expect {
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).twice
end
it "passes with :thrice count" do
expect {
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).thrice
end
it "passes with at_least when enqueued emails are over the limit" do
expect {
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).at_least(:once)
end
it "passes with at_most when enqueued emails are under the limit" do
expect {
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).at_most(:twice)
end
it "generates a failure message when given 0 argument" do
expect {
expect { }.to have_enqueued_mail.at_least(:once)
}.to raise_error(/expected to enqueue ActionMailer::Base at least 1 time but enqueued 0/)
end
it "generates a failure message when given only mailer argument" do
expect {
expect { }.to have_enqueued_mail(TestMailer).at_least(:once)
}.to raise_error(/expected to enqueue TestMailer at least 1 time but enqueued 0/)
end
it "generates a failure message with at least hint" do
expect {
expect { }.to have_enqueued_mail(TestMailer, :test_email).at_least(:once)
}.to raise_error(/expected to enqueue TestMailer.test_email at least 1 time but enqueued 0/)
end
it "generates a failure message with at most hint" do
expect {
expect {
TestMailer.test_email.deliver_later
TestMailer.test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).at_most(:once)
}.to raise_error(/expected to enqueue TestMailer.test_email at most 1 time but enqueued 2/)
end
it "passes for mailer methods that accept arguments when the provided argument matcher is not used" do
expect {
TestMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args)
end
it "passes for mailer methods with default arguments" do
expect {
TestMailer.email_with_optional_args('required').deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_optional_args)
expect {
TestMailer.email_with_optional_args('required').deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_optional_args).with('required')
expect {
TestMailer.email_with_optional_args('required', 'optional').deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_optional_args).with('required', 'optional')
end
it "passes with provided argument matchers" do
expect {
TestMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1, 2)
expect {
TestMailer.email_with_args(1, 2).deliver_later
}.not_to have_enqueued_mail(TestMailer, :email_with_args).with(3, 4)
end
it "generates a failure message" do
expect {
expect { }.to have_enqueued_email(TestMailer, :test_email)
}.to raise_error(/expected to enqueue TestMailer.test_email/)
end
it "generates a failure message with arguments" do
expect {
expect { }.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
}.to raise_error(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\], but enqueued 0/)
end
it "passes when deliver_later is called with a wait_until argument" do
send_time = Date.tomorrow.noon
expect {
TestMailer.test_email.deliver_later(wait_until: send_time)
}.to have_enqueued_email(TestMailer, :test_email).at(send_time)
end
it "generates a failure message with at" do
send_time = Date.tomorrow.noon
expect {
expect {
TestMailer.test_email.deliver_later(wait_until: send_time + 1)
}.to have_enqueued_email(TestMailer, :test_email).at(send_time)
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time at #{send_time.strftime('%F %T')}/)
end
it "accepts composable matchers as an at date" do
future = 1.minute.from_now
slightly_earlier = 58.seconds.from_now
expect {
TestMailer.test_email.deliver_later(wait_until: slightly_earlier)
}.to have_enqueued_email(TestMailer, :test_email).at(a_value_within(5.seconds).of(future))
end
it "passes when deliver_later is called with a queue argument" do
expect {
TestMailer.test_email.deliver_later(queue: 'urgent_mail')
}.to have_enqueued_email(TestMailer, :test_email).on_queue('urgent_mail')
end
it "generates a failure message with on_queue" do
expect {
expect {
TestMailer.test_email.deliver_later(queue: 'not_urgent_mail')
}.to have_enqueued_email(TestMailer, :test_email).on_queue('urgent_mail')
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time on queue urgent_mail/)
end
it "generates a failure message with unmatching enqueued mail jobs" do
non_mailer_job = Class.new(ActiveJob::Base) do
def perform; end
def self.name; "NonMailerJob"; end
end
send_time = Date.tomorrow.noon
queue = 'urgent_mail'
message = "expected to enqueue TestMailer.email_with_args exactly 1 time with [1, 2], but enqueued 0" \
"\nQueued deliveries:" \
"\n TestMailer.test_email" \
"\n TestMailer.email_with_args with [3, 4], on queue #{queue}, at #{send_time}"
expect {
expect {
non_mailer_job.perform_later
TestMailer.test_email.deliver_later
TestMailer.email_with_args(3, 4).deliver_later(wait_until: send_time, queue: queue)
}.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
}.to raise_error(message)
end
it "throws descriptive error when no test adapter set" do
queue_adapter = ActiveJob::Base.queue_adapter
ActiveJob::Base.queue_adapter = :inline
expect {
expect { TestMailer.test_email.deliver_later }.to have_enqueued_mail(TestMailer, :test_email)
}.to raise_error("To use HaveEnqueuedMail matcher set `ActiveJob::Base.queue_adapter = :test`")
ActiveJob::Base.queue_adapter = queue_adapter
end
it "fails with with block with incorrect data" do
expect {
expect {
TestMailer.email_with_args('asdf', 'zxcv').deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args).with { |first_arg, _second_arg|
expect(first_arg).to eq("zxcv")
}
}.to raise_error { |e|
expect(e.message).to match(/expected: "zxcv"/)
expect(e.message).to match(/got: "asdf"/)
}
end
it "passes multiple arguments to with block" do
expect {
TestMailer.email_with_args('asdf', 'zxcv').deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args).with { |first_arg, second_arg|
expect(first_arg).to eq("asdf")
expect(second_arg).to eq("zxcv")
}
end
it "only calls with block if other conditions are met" do
noon = Date.tomorrow.noon
midnight = Date.tomorrow.midnight
expect {
TestMailer.email_with_args('high', 'noon').deliver_later(wait_until: noon)
TestMailer.email_with_args('midnight', 'rider').deliver_later(wait_until: midnight)
}.to have_enqueued_mail(TestMailer, :email_with_args).at(noon).with { |first_arg, second_arg|
expect(first_arg).to eq('high')
expect(second_arg).to eq('noon')
}
end
context 'when parameterized', skip: !RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? do
it "passes when mailer is parameterized" do
expect {
TestMailer.with('foo' => 'bar').test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email)
end
it "passes when mixing parameterized and non-parameterized emails" do
expect {
TestMailer.with('foo' => 'bar').test_email.deliver_later
TestMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).and have_enqueued_mail(TestMailer, :email_with_args)
end
it "passes with provided argument matchers" do
expect {
TestMailer.with('foo' => 'bar').test_email.deliver_later
}.to have_enqueued_mail(TestMailer, :test_email).with('foo' => 'bar')
expect {
TestMailer.with('foo' => 'bar').email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args).with({'foo' => 'bar'}, 1, 2)
end
end
context 'mailer job is unified', skip: !RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? do
it "passes when mailer is parameterized" do
expect {
UnifiedMailer.with('foo' => 'bar').test_email.deliver_later
}.to have_enqueued_mail(UnifiedMailer, :test_email)
end
it "passes when mixing parameterized and non-parameterized emails" do
expect {
UnifiedMailer.with('foo' => 'bar').test_email.deliver_later
UnifiedMailer.email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(UnifiedMailer, :test_email).and have_enqueued_mail(UnifiedMailer, :email_with_args)
end
it "passes with provided argument matchers" do
expect {
UnifiedMailer.with('foo' => 'bar').test_email.deliver_later
}.to have_enqueued_mail(UnifiedMailer, :test_email).with(
a_hash_including(params: {'foo' => 'bar'})
)
expect {
UnifiedMailer.with('foo' => 'bar').email_with_args(1, 2).deliver_later
}.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(
a_hash_including(params: {'foo' => 'bar'}, args: [1, 2])
)
end
it "passes when using a mailer with `delivery_job` set to a sub class of `ActionMailer::DeliveryJob`" do
expect {
UnifiedMailerWithDeliveryJobSubClass.test_email.deliver_later
}.to have_enqueued_mail(UnifiedMailerWithDeliveryJobSubClass, :test_email)
end
end
end
end