Skip to content

Commit 9a52474

Browse files
committed
Refaactor to use fail_with matcher
1 parent d3bbe58 commit 9a52474

File tree

3 files changed

+43
-60
lines changed

3 files changed

+43
-60
lines changed

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def perform; raise StandardError; end
152152
it "fails when job is not enqueued" do
153153
expect {
154154
expect { }.to have_enqueued_job
155-
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 0/)
155+
}.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 0/)
156156
end
157157

158158
it "fails when too many jobs enqueued" do
@@ -161,20 +161,20 @@ def perform; raise StandardError; end
161161
heavy_lifting_job.perform_later
162162
heavy_lifting_job.perform_later
163163
}.to have_enqueued_job.exactly(1)
164-
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 2/)
164+
}.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 2/)
165165
end
166166

167167
it "reports correct number in fail error message" do
168168
heavy_lifting_job.perform_later
169169
expect {
170170
expect { }.to have_enqueued_job.exactly(1)
171-
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 0/)
171+
}.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 0/)
172172
end
173173

174174
it "fails when negated and job is enqueued" do
175175
expect {
176176
expect { heavy_lifting_job.perform_later }.not_to have_enqueued_job
177-
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 1/)
177+
}.to fail_with(/expected not to enqueue at least 1 jobs, but enqueued 1/)
178178
end
179179

180180
it "fails when negated and several jobs enqueued" do
@@ -183,7 +183,7 @@ def perform; raise StandardError; end
183183
heavy_lifting_job.perform_later
184184
heavy_lifting_job.perform_later
185185
}.not_to have_enqueued_job
186-
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
186+
}.to fail_with(/expected not to enqueue at least 1 jobs, but enqueued 2/)
187187
end
188188

189189
it "passes with job name" do
@@ -238,7 +238,7 @@ def perform; raise StandardError; end
238238
it "generates failure message with at least hint" do
239239
expect {
240240
expect { }.to have_enqueued_job.at_least(:once)
241-
}.to raise_error(/expected to enqueue at least 1 jobs, but enqueued 0/)
241+
}.to fail_with(/expected to enqueue at least 1 jobs, but enqueued 0/)
242242
end
243243

244244
it "generates failure message with at most hint" do
@@ -247,7 +247,7 @@ def perform; raise StandardError; end
247247
hello_job.perform_later
248248
hello_job.perform_later
249249
}.to have_enqueued_job.at_most(:once)
250-
}.to raise_error(/expected to enqueue at most 1 jobs, but enqueued 2/)
250+
}.to fail_with(/expected to enqueue at most 1 jobs, but enqueued 2/)
251251
end
252252

253253
it "passes with provided queue name as string" do
@@ -291,7 +291,7 @@ def perform; raise StandardError; end
291291
travel_to time do
292292
expect {
293293
expect { hello_job.set(wait: 5).perform_later }.to have_enqueued_job.at(time + 5)
294-
}.to raise_error(/expected to enqueue exactly 1 jobs/)
294+
}.to fail_with(/expected to enqueue exactly 1 jobs/)
295295
end
296296
end
297297

@@ -315,7 +315,7 @@ def perform; raise StandardError; end
315315
expect {
316316
hello_job.perform_later
317317
}.to have_enqueued_job.at(date)
318-
}.to raise_error(/expected to enqueue exactly 1 jobs, at .+ but enqueued 0/)
318+
}.to fail_with(/expected to enqueue exactly 1 jobs, at .+ but enqueued 0/)
319319
end
320320

321321
it "has an enqueued job when not providing at and there is a wait" do
@@ -343,21 +343,15 @@ def perform; raise StandardError; end
343343
expect {
344344
two_args_job.perform_later(1)
345345
}.to have_enqueued_job.with(1)
346-
}.to raise_error(
347-
RSpec::Expectations::ExpectationNotMetError,
348-
/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/
349-
)
346+
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
350347
end
351348

352349
it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do
353350
expect {
354351
expect {
355352
keyword_args_job.perform_later(1, 2)
356353
}.to have_enqueued_job.with(1, 2)
357-
}.to raise_error(
358-
RSpec::Expectations::ExpectationNotMetError,
359-
/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/
360-
)
354+
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
361355
end
362356

363357
it "passes with provided arguments containing global id object" do
@@ -384,7 +378,7 @@ def perform; raise StandardError; end
384378
expect {
385379
hello_job.perform_later(1)
386380
}.to have_enqueued_job(hello_job).with(42).on_queue("low").at(date).exactly(2).times
387-
}.to raise_error(message)
381+
}.to fail_with(message)
388382
end
389383

390384
it "throws descriptive error when no test adapter set" do
@@ -490,37 +484,31 @@ def perform; raise StandardError; end
490484
it "fails when job is not enqueued" do
491485
expect {
492486
expect(heavy_lifting_job).to have_been_enqueued
493-
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 0/)
487+
}.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 0/)
494488
end
495489

496490
it "fails if the arguments do not match the job's signature" do
497491
two_args_job.perform_later(1)
498492

499493
expect {
500494
expect(two_args_job).to have_been_enqueued.with(1)
501-
}.to raise_error(
502-
RSpec::Expectations::ExpectationNotMetError,
503-
/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/
504-
)
495+
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
505496
end
506497

507498
it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do
508499
keyword_args_job.perform_later(1, 2)
509500

510501
expect {
511502
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
512-
}.to raise_error(
513-
RSpec::Expectations::ExpectationNotMetError,
514-
/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/
515-
)
503+
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
516504
end
517505

518506
it "fails when negated and several jobs enqueued" do
519507
heavy_lifting_job.perform_later
520508
heavy_lifting_job.perform_later
521509
expect {
522510
expect(heavy_lifting_job).not_to have_been_enqueued
523-
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
511+
}.to fail_with(/expected not to enqueue at least 1 jobs, but enqueued 2/)
524512
end
525513

526514
it "accepts composable matchers as an at date" do
@@ -569,7 +557,7 @@ def perform; raise StandardError; end
569557
it "fails when job is not performed" do
570558
expect {
571559
expect { }.to have_performed_job
572-
}.to raise_error(/expected to perform exactly 1 jobs, but performed 0/)
560+
}.to fail_with(/expected to perform exactly 1 jobs, but performed 0/)
573561
end
574562

575563
it "fails when too many jobs performed" do
@@ -578,20 +566,20 @@ def perform; raise StandardError; end
578566
heavy_lifting_job.perform_later
579567
heavy_lifting_job.perform_later
580568
}.to have_performed_job.exactly(1)
581-
}.to raise_error(/expected to perform exactly 1 jobs, but performed 2/)
569+
}.to fail_with(/expected to perform exactly 1 jobs, but performed 2/)
582570
end
583571

584572
it "reports correct number in fail error message" do
585573
heavy_lifting_job.perform_later
586574
expect {
587575
expect { }.to have_performed_job.exactly(1)
588-
}.to raise_error(/expected to perform exactly 1 jobs, but performed 0/)
576+
}.to fail_with(/expected to perform exactly 1 jobs, but performed 0/)
589577
end
590578

591579
it "fails when negated and job is performed" do
592580
expect {
593581
expect { heavy_lifting_job.perform_later }.not_to have_performed_job
594-
}.to raise_error(/expected not to perform exactly 1 jobs, but performed 1/)
582+
}.to fail_with(/expected not to perform exactly 1 jobs, but performed 1/)
595583
end
596584

597585
it "passes with job name" do
@@ -646,7 +634,7 @@ def perform; raise StandardError; end
646634
it "generates failure message with at least hint" do
647635
expect {
648636
expect { }.to have_performed_job.at_least(:once)
649-
}.to raise_error(/expected to perform at least 1 jobs, but performed 0/)
637+
}.to fail_with(/expected to perform at least 1 jobs, but performed 0/)
650638
end
651639

652640
it "generates failure message with at most hint" do
@@ -655,7 +643,7 @@ def perform; raise StandardError; end
655643
hello_job.perform_later
656644
hello_job.perform_later
657645
}.to have_performed_job.at_most(:once)
658-
}.to raise_error(/expected to perform at most 1 jobs, but performed 2/)
646+
}.to fail_with(/expected to perform at most 1 jobs, but performed 2/)
659647
end
660648

661649
it "passes with provided queue name as string" do
@@ -707,7 +695,7 @@ def perform; raise StandardError; end
707695
expect {
708696
hello_job.perform_later(1)
709697
}.to have_performed_job(hello_job).with(42).on_queue("low").at(date).exactly(2).times
710-
}.to raise_error(message)
698+
}.to fail_with(message)
711699
end
712700

713701
it "throws descriptive error when no test adapter set" do
@@ -792,7 +780,7 @@ def perform; raise StandardError; end
792780
it "fails when job is not performed" do
793781
expect {
794782
expect(heavy_lifting_job).to have_been_performed
795-
}.to raise_error(/expected to perform exactly 1 jobs, but performed 0/)
783+
}.to fail_with(/expected to perform exactly 1 jobs, but performed 0/)
796784
end
797785
end
798786

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_email; end
110110
expect {
111111
AnotherTestMailer.test_email.deliver_later
112112
}.to have_enqueued_mail(TestMailer)
113-
}.to raise_error(/expected to enqueue TestMailer exactly 1 time but enqueued 0/)
113+
}.to fail_with(/expected to enqueue TestMailer exactly 1 time but enqueued 0/)
114114
end
115115

116116
it "counts only emails enqueued in the block" do
@@ -127,7 +127,7 @@ def test_email; end
127127
TestMailer.test_email.deliver_later
128128
TestMailer.test_email.deliver_later
129129
}.to have_enqueued_mail(TestMailer, :test_email).exactly(1)
130-
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time/)
130+
}.to fail_with(/expected to enqueue TestMailer.test_email exactly 1 time/)
131131
end
132132

133133
it "matches based on mailer class and method name" do
@@ -149,7 +149,7 @@ def test_email; end
149149
expect {
150150
TestMailer.test_email.deliver_later
151151
}.not_to have_enqueued_mail(TestMailer, :test_email)
152-
}.to raise_error(/expected not to enqueue TestMailer.test_email at least 1 time but enqueued 1/)
152+
}.to fail_with(/expected not to enqueue TestMailer.test_email at least 1 time but enqueued 1/)
153153
end
154154

155155
it "passes with :once count" do
@@ -189,19 +189,19 @@ def test_email; end
189189
it "generates a failure message when given 0 argument" do
190190
expect {
191191
expect { }.to have_enqueued_mail.at_least(:once)
192-
}.to raise_error(/expected to enqueue ActionMailer::Base at least 1 time but enqueued 0/)
192+
}.to fail_with(/expected to enqueue ActionMailer::Base at least 1 time but enqueued 0/)
193193
end
194194

195195
it "generates a failure message when given only mailer argument" do
196196
expect {
197197
expect { }.to have_enqueued_mail(TestMailer).at_least(:once)
198-
}.to raise_error(/expected to enqueue TestMailer at least 1 time but enqueued 0/)
198+
}.to fail_with(/expected to enqueue TestMailer at least 1 time but enqueued 0/)
199199
end
200200

201201
it "generates a failure message with at least hint" do
202202
expect {
203203
expect { }.to have_enqueued_mail(TestMailer, :test_email).at_least(:once)
204-
}.to raise_error(/expected to enqueue TestMailer.test_email at least 1 time but enqueued 0/)
204+
}.to fail_with(/expected to enqueue TestMailer.test_email at least 1 time but enqueued 0/)
205205
end
206206

207207
it "generates a failure message with at most hint" do
@@ -210,7 +210,7 @@ def test_email; end
210210
TestMailer.test_email.deliver_later
211211
TestMailer.test_email.deliver_later
212212
}.to have_enqueued_mail(TestMailer, :test_email).at_most(:once)
213-
}.to raise_error(/expected to enqueue TestMailer.test_email at most 1 time but enqueued 2/)
213+
}.to fail_with(/expected to enqueue TestMailer.test_email at most 1 time but enqueued 2/)
214214
end
215215

216216
it "passes for mailer methods that accept arguments when the provided argument matcher is not used" do
@@ -248,22 +248,19 @@ def test_email; end
248248
expect {
249249
TestMailer.email_with_args(1).deliver_later
250250
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
251-
}.to raise_error(
252-
RSpec::Expectations::ExpectationNotMetError,
253-
/Incorrect arguments passed to TestMailer: Wrong number of arguments/
254-
)
251+
}.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/)
255252
end
256253

257254
it "generates a failure message" do
258255
expect {
259256
expect { }.to have_enqueued_email(TestMailer, :test_email)
260-
}.to raise_error(/expected to enqueue TestMailer.test_email/)
257+
}.to fail_with(/expected to enqueue TestMailer.test_email/)
261258
end
262259

263260
it "generates a failure message with arguments" do
264261
expect {
265262
expect { }.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
266-
}.to raise_error(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\], but enqueued 0/)
263+
}.to fail_with(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\], but enqueued 0/)
267264
end
268265

269266
it "passes when deliver_later is called with a wait_until argument" do
@@ -281,7 +278,7 @@ def test_email; end
281278
expect {
282279
TestMailer.test_email.deliver_later(wait_until: send_time + 1)
283280
}.to have_enqueued_email(TestMailer, :test_email).at(send_time)
284-
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time at #{send_time.strftime('%F %T')}/)
281+
}.to fail_with(/expected to enqueue TestMailer.test_email exactly 1 time at #{send_time.strftime('%F %T')}/)
285282
end
286283

287284
it "accepts composable matchers as an at date" do
@@ -304,7 +301,7 @@ def test_email; end
304301
expect {
305302
TestMailer.test_email.deliver_later(queue: 'not_urgent_mail')
306303
}.to have_enqueued_email(TestMailer, :test_email).on_queue('urgent_mail')
307-
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time on queue urgent_mail/)
304+
}.to fail_with(/expected to enqueue TestMailer.test_email exactly 1 time on queue urgent_mail/)
308305
end
309306

310307
it "generates a failure message with unmatching enqueued mail jobs" do
@@ -327,7 +324,7 @@ def self.name; "NonMailerJob"; end
327324
TestMailer.test_email.deliver_later
328325
TestMailer.email_with_args(3, 4).deliver_later(wait_until: send_time, queue: queue)
329326
}.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
330-
}.to raise_error(message)
327+
}.to fail_with(message)
331328
end
332329

333330
it "throws descriptive error when no test adapter set" do
@@ -405,10 +402,7 @@ def self.name; "NonMailerJob"; end
405402
expect {
406403
TestMailer.with('foo' => 'bar').email_with_args(1).deliver_later
407404
}.to have_enqueued_mail(TestMailer, :email_with_args).with({ 'foo' => 'bar' }, 1)
408-
}.to raise_error(
409-
RSpec::Expectations::ExpectationNotMetError,
410-
/Incorrect arguments passed to TestMailer: Wrong number of arguments/
411-
)
405+
}.to raise_error(/Incorrect arguments passed to TestMailer: Wrong number of arguments/)
412406
end
413407
end
414408

@@ -465,10 +459,7 @@ def self.name; "NonMailerJob"; end
465459
}.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(
466460
a_hash_including(params: { 'foo' => 'bar' }, args: [1])
467461
)
468-
}.to raise_error(
469-
RSpec::Expectations::ExpectationNotMetError,
470-
/Incorrect arguments passed to UnifiedMailer: Wrong number of arguments/
471-
)
462+
}.to fail_with(/Incorrect arguments passed to UnifiedMailer: Wrong number of arguments/)
472463
end
473464
end
474465
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def self.world=(world)
2424

2525
require 'rspec/support/spec'
2626
require 'rspec/core/sandbox'
27+
require 'rspec/matchers/fail_matchers'
2728
require 'rspec/rails'
2829
require 'ammeter/init'
2930

@@ -37,6 +38,9 @@ def self.run_all(reporter = nil)
3738

3839
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
3940
RSpec.configure do |config|
41+
# Bring in the failure matchers from rspec-expectations
42+
config.include RSpec::Matchers::FailMatchers
43+
4044
config.expect_with :rspec do |expectations|
4145
# include_chain_clauses_in_custom_matcher_descriptions is removed in RSpec Expectations 4
4246
expectations.include_chain_clauses_in_custom_matcher_descriptions = true if expectations.respond_to?(:include_chain_clauses_in_custom_matcher_descriptions=)

0 commit comments

Comments
 (0)