Skip to content

Commit b819663

Browse files
authored
Merge pull request #2808 from odlp/pr-suppress-job-and-mail-signature-checks
Respect verify partial doubles config when verifying job/mailer arguments
2 parents 1ba134d + 19baec8 commit b819663

File tree

4 files changed

+118
-27
lines changed

4 files changed

+118
-27
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def arguments_match?(job)
178178
end
179179

180180
def detect_args_signature_mismatch(jobs)
181+
return if skip_signature_verification?
182+
181183
jobs.each do |job|
182184
args = deserialize_arguments(job)
183185

@@ -189,6 +191,11 @@ def detect_args_signature_mismatch(jobs)
189191
nil
190192
end
191193

194+
def skip_signature_verification?
195+
!RSpec::Mocks.configuration.verify_partial_doubles? ||
196+
RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification
197+
end
198+
192199
def check_args_signature_mismatch(job_class, job_method, args)
193200
signature = Support::MethodSignature.new(job_class.public_instance_method(job_method))
194201
verifier = Support::StrictSignatureVerifier.new(signature, args)

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def arguments_match?(job)
9393

9494
def detect_args_signature_mismatch(jobs)
9595
return if @method_name.nil?
96+
return if skip_signature_verification?
9697

9798
mailer_class = mailer_class_name.constantize
9899

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def self.find(_id)
4242
ActiveJob::Base.logger = original_logger
4343
end
4444

45+
around do |example|
46+
original_value = RSpec::Mocks.configuration.verify_partial_doubles?
47+
example.run
48+
ensure
49+
RSpec::Mocks.configuration.verify_partial_doubles = original_value
50+
end
51+
4552
let(:heavy_lifting_job) do
4653
Class.new(ActiveJob::Base) do
4754
def perform; end
@@ -372,20 +379,42 @@ def perform; raise StandardError; end
372379
}.to have_enqueued_job.with(42, "David")
373380
end
374381

375-
it "fails if the arguments do not match the job's signature" do
376-
expect {
382+
describe "verifying the arguments passed match the job's signature" do
383+
it "fails if there is an arity mismatch" do
377384
expect {
378-
two_args_job.perform_later(1)
379-
}.to have_enqueued_job.with(1)
380-
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
381-
end
385+
expect {
386+
two_args_job.perform_later(1)
387+
}.to have_enqueued_job.with(1)
388+
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
389+
end
382390

383-
it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do
384-
expect {
391+
it "fails if there is a keyword/positional arguments mismatch" do
385392
expect {
386-
keyword_args_job.perform_later(1, 2)
387-
}.to have_enqueued_job.with(1, 2)
388-
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
393+
expect {
394+
keyword_args_job.perform_later(1, 2)
395+
}.to have_enqueued_job.with(1, 2)
396+
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
397+
end
398+
399+
context "with partial double verification disabled" do
400+
before do
401+
RSpec::Mocks.configuration.verify_partial_doubles = false
402+
end
403+
404+
it "skips signature checks" do
405+
expect { two_args_job.perform_later(1) }.to have_enqueued_job.with(1)
406+
end
407+
end
408+
409+
context "when partial double verification is temporarily suspended" do
410+
it "skips signature checks" do
411+
without_partial_double_verification {
412+
expect {
413+
two_args_job.perform_later(1)
414+
}.to have_enqueued_job.with(1)
415+
}
416+
end
417+
end
389418
end
390419

391420
it "passes with provided arguments containing global id object" do
@@ -521,20 +550,44 @@ def perform; raise StandardError; end
521550
}.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 0/)
522551
end
523552

524-
it "fails if the arguments do not match the job's signature" do
525-
two_args_job.perform_later(1)
553+
describe "verifying the arguments passed match the job's signature" do
554+
it "fails if there is an arity mismatch" do
555+
two_args_job.perform_later(1)
526556

527-
expect {
528-
expect(two_args_job).to have_been_enqueued.with(1)
529-
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
530-
end
557+
expect {
558+
expect(two_args_job).to have_been_enqueued.with(1)
559+
}.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/)
560+
end
531561

532-
it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do
533-
keyword_args_job.perform_later(1, 2)
562+
it "fails if there is a keyword/positional arguments mismatch" do
563+
keyword_args_job.perform_later(1, 2)
534564

535-
expect {
536-
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
537-
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
565+
expect {
566+
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
567+
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
568+
end
569+
570+
context "with partial double verification disabled" do
571+
before do
572+
RSpec::Mocks.configuration.verify_partial_doubles = false
573+
end
574+
575+
it "skips signature checks" do
576+
keyword_args_job.perform_later(1, 2)
577+
578+
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
579+
end
580+
end
581+
582+
context "when partial double verification is temporarily suspended" do
583+
it "skips signature checks" do
584+
keyword_args_job.perform_later(1, 2)
585+
586+
without_partial_double_verification {
587+
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
588+
}
589+
end
590+
end
538591
end
539592

540593
it "fails when negated and several jobs enqueued" do

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def test_email; end
6060
ActiveJob::Base.logger = original_logger
6161
end
6262

63+
around do |example|
64+
original_value = RSpec::Mocks.configuration.verify_partial_doubles?
65+
example.run
66+
ensure
67+
RSpec::Mocks.configuration.verify_partial_doubles = original_value
68+
end
69+
6370
describe "have_enqueued_mail" do
6471
it "passes when a mailer method is called with deliver_later" do
6572
expect {
@@ -251,12 +258,35 @@ def test_email; end
251258
}.not_to have_enqueued_mail(TestMailer, :email_with_args).with(3, 4)
252259
end
253260

254-
it "fails if the arguments do not match the mailer method's signature" do
255-
expect {
261+
describe "verifying the arguments passed match the mailer's signature" do
262+
it "fails if there is a mismatch" do
256263
expect {
257-
TestMailer.email_with_args(1).deliver_later
258-
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
259-
}.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/)
264+
expect {
265+
TestMailer.email_with_args(1).deliver_later
266+
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
267+
}.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/)
268+
end
269+
270+
context "with partial double verification disabled" do
271+
before do
272+
RSpec::Mocks.configuration.verify_partial_doubles = false
273+
end
274+
275+
it "skips signature checks" do
276+
expect { TestMailer.email_with_args(1).deliver_later }
277+
.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
278+
end
279+
end
280+
281+
context "when partial double verification is temporarily suspended" do
282+
it "skips signature checks" do
283+
without_partial_double_verification {
284+
expect {
285+
TestMailer.email_with_args(1).deliver_later
286+
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
287+
}
288+
end
289+
end
260290
end
261291

262292
it "generates a failure message" do

0 commit comments

Comments
 (0)