Skip to content

Commit 2f519d4

Browse files
committed
Respect #verify_partial_doubles? config for job/mailer signature verification
#2801 (comment)
1 parent a43702d commit 2f519d4

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 2 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 unless RSpec::Mocks.configuration.verify_partial_doubles?
182+
181183
jobs.each do |job|
182184
args = deserialize_arguments(job)
183185

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 unless RSpec::Mocks.configuration.verify_partial_doubles?
9697

9798
mailer_class = mailer_class_name.constantize
9899

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "rspec/rails/feature_check"
2+
require "support/temporary_assignment"
23

34
if RSpec::Rails::FeatureCheck.has_active_job?
45
require "rspec/rails/matchers/active_job"
@@ -34,6 +35,7 @@ def self.find(_id)
3435

3536
RSpec.describe "ActiveJob matchers", skip: !RSpec::Rails::FeatureCheck.has_active_job? do
3637
include ActiveSupport::Testing::TimeHelpers
38+
include TemporaryAssignment
3739

3840
around do |example|
3941
original_logger = ActiveJob::Base.logger
@@ -388,6 +390,14 @@ def perform; raise StandardError; end
388390
}.to have_enqueued_job.with(1, 2)
389391
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
390392
end
393+
394+
context "with partial double verification disabled" do
395+
it "skips signature checks" do
396+
with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
397+
expect { two_args_job.perform_later(1) }.to have_enqueued_job.with(1)
398+
}
399+
end
400+
end
391401
end
392402

393403
it "passes with provided arguments containing global id object" do
@@ -539,6 +549,16 @@ def perform; raise StandardError; end
539549
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
540550
}.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/)
541551
end
552+
553+
context "with partial double verification disabled" do
554+
it "skips signature checks" do
555+
keyword_args_job.perform_later(1, 2)
556+
557+
with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
558+
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
559+
}
560+
end
561+
end
542562
end
543563

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

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "rspec/rails/feature_check"
2+
require "support/temporary_assignment"
23

34
if RSpec::Rails::FeatureCheck.has_active_job?
45
require "action_mailer"
@@ -49,6 +50,8 @@ def test_email; end
4950
end
5051

5152
RSpec.describe "HaveEnqueuedMail matchers", skip: !RSpec::Rails::FeatureCheck.has_active_job? do
53+
include TemporaryAssignment
54+
5255
before do
5356
ActiveJob::Base.queue_adapter = :test
5457
end
@@ -259,6 +262,16 @@ def test_email; end
259262
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
260263
}.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/)
261264
end
265+
266+
context "with partial double verification disabled" do
267+
it "skips signature checks" do
268+
with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
269+
expect {
270+
TestMailer.email_with_args(1).deliver_later
271+
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
272+
}
273+
end
274+
end
262275
end
263276

264277
it "generates a failure message" do
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module TemporaryAssignment
4+
def with_temporary_assignment(assignee, attribute, temporary_value)
5+
predicate = "#{attribute}?"
6+
attribute_reader = assignee.respond_to?(predicate) ? predicate : attribute
7+
attribute_writer = "#{attribute}="
8+
9+
original_value = assignee.public_send(attribute_reader)
10+
assignee.public_send(attribute_writer, temporary_value)
11+
yield
12+
ensure
13+
assignee.public_send(attribute_writer, original_value)
14+
end
15+
end

0 commit comments

Comments
 (0)