diff --git a/lib/rspec/rails/matchers/active_job.rb b/lib/rspec/rails/matchers/active_job.rb index 40c676c0c..1bdd323e5 100644 --- a/lib/rspec/rails/matchers/active_job.rb +++ b/lib/rspec/rails/matchers/active_job.rb @@ -178,6 +178,8 @@ def arguments_match?(job) end def detect_args_signature_mismatch(jobs) + return if skip_signature_verification? + jobs.each do |job| args = deserialize_arguments(job) @@ -189,6 +191,11 @@ def detect_args_signature_mismatch(jobs) nil end + def skip_signature_verification? + !RSpec::Mocks.configuration.verify_partial_doubles? || + RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification + end + def check_args_signature_mismatch(job_class, job_method, args) signature = Support::MethodSignature.new(job_class.public_instance_method(job_method)) verifier = Support::StrictSignatureVerifier.new(signature, args) diff --git a/lib/rspec/rails/matchers/have_enqueued_mail.rb b/lib/rspec/rails/matchers/have_enqueued_mail.rb index 85aaea274..cf4576e57 100644 --- a/lib/rspec/rails/matchers/have_enqueued_mail.rb +++ b/lib/rspec/rails/matchers/have_enqueued_mail.rb @@ -93,6 +93,7 @@ def arguments_match?(job) def detect_args_signature_mismatch(jobs) return if @method_name.nil? + return if skip_signature_verification? mailer_class = mailer_class_name.constantize diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 86e191ae5..5b299a28b 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -42,6 +42,13 @@ def self.find(_id) ActiveJob::Base.logger = original_logger end + around do |example| + original_value = RSpec::Mocks.configuration.verify_partial_doubles? + example.run + ensure + RSpec::Mocks.configuration.verify_partial_doubles = original_value + end + let(:heavy_lifting_job) do Class.new(ActiveJob::Base) do def perform; end @@ -372,20 +379,42 @@ def perform; raise StandardError; end }.to have_enqueued_job.with(42, "David") end - it "fails if the arguments do not match the job's signature" do - expect { + describe "verifying the arguments passed match the job's signature" do + it "fails if there is an arity mismatch" do expect { - two_args_job.perform_later(1) - }.to have_enqueued_job.with(1) - }.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/) - end + expect { + two_args_job.perform_later(1) + }.to have_enqueued_job.with(1) + }.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/) + end - it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do - expect { + it "fails if there is a keyword/positional arguments mismatch" do expect { - keyword_args_job.perform_later(1, 2) - }.to have_enqueued_job.with(1, 2) - }.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/) + expect { + keyword_args_job.perform_later(1, 2) + }.to have_enqueued_job.with(1, 2) + }.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/) + end + + context "with partial double verification disabled" do + before do + RSpec::Mocks.configuration.verify_partial_doubles = false + end + + it "skips signature checks" do + expect { two_args_job.perform_later(1) }.to have_enqueued_job.with(1) + end + end + + context "when partial double verification is temporarily suspended" do + it "skips signature checks" do + without_partial_double_verification { + expect { + two_args_job.perform_later(1) + }.to have_enqueued_job.with(1) + } + end + end end it "passes with provided arguments containing global id object" do @@ -521,20 +550,44 @@ def perform; raise StandardError; end }.to fail_with(/expected to enqueue exactly 1 jobs, but enqueued 0/) end - it "fails if the arguments do not match the job's signature" do - two_args_job.perform_later(1) + describe "verifying the arguments passed match the job's signature" do + it "fails if there is an arity mismatch" do + two_args_job.perform_later(1) - expect { - expect(two_args_job).to have_been_enqueued.with(1) - }.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/) - end + expect { + expect(two_args_job).to have_been_enqueued.with(1) + }.to fail_with(/Incorrect arguments passed to TwoArgsJob: Wrong number of arguments/) + end - it "fails if the job's signature/arguments are mismatched keyword/positional arguments" do - keyword_args_job.perform_later(1, 2) + it "fails if there is a keyword/positional arguments mismatch" do + keyword_args_job.perform_later(1, 2) - expect { - expect(keyword_args_job).to have_been_enqueued.with(1, 2) - }.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/) + expect { + expect(keyword_args_job).to have_been_enqueued.with(1, 2) + }.to fail_with(/Incorrect arguments passed to KeywordArgsJob: Missing required keyword arguments/) + end + + context "with partial double verification disabled" do + before do + RSpec::Mocks.configuration.verify_partial_doubles = false + end + + it "skips signature checks" do + keyword_args_job.perform_later(1, 2) + + expect(keyword_args_job).to have_been_enqueued.with(1, 2) + end + end + + context "when partial double verification is temporarily suspended" do + it "skips signature checks" do + keyword_args_job.perform_later(1, 2) + + without_partial_double_verification { + expect(keyword_args_job).to have_been_enqueued.with(1, 2) + } + end + end end it "fails when negated and several jobs enqueued" do diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb index 14b886189..d2cc57ba4 100644 --- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb +++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb @@ -60,6 +60,13 @@ def test_email; end ActiveJob::Base.logger = original_logger end + around do |example| + original_value = RSpec::Mocks.configuration.verify_partial_doubles? + example.run + ensure + RSpec::Mocks.configuration.verify_partial_doubles = original_value + end + describe "have_enqueued_mail" do it "passes when a mailer method is called with deliver_later" do expect { @@ -251,12 +258,35 @@ def test_email; end }.not_to have_enqueued_mail(TestMailer, :email_with_args).with(3, 4) end - it "fails if the arguments do not match the mailer method's signature" do - expect { + describe "verifying the arguments passed match the mailer's signature" do + it "fails if there is a mismatch" do expect { - TestMailer.email_with_args(1).deliver_later - }.to have_enqueued_mail(TestMailer, :email_with_args).with(1) - }.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/) + expect { + TestMailer.email_with_args(1).deliver_later + }.to have_enqueued_mail(TestMailer, :email_with_args).with(1) + }.to fail_with(/Incorrect arguments passed to TestMailer: Wrong number of arguments/) + end + + context "with partial double verification disabled" do + before do + RSpec::Mocks.configuration.verify_partial_doubles = false + end + + it "skips signature checks" do + expect { TestMailer.email_with_args(1).deliver_later } + .to have_enqueued_mail(TestMailer, :email_with_args).with(1) + end + end + + context "when partial double verification is temporarily suspended" do + it "skips signature checks" do + without_partial_double_verification { + expect { + TestMailer.email_with_args(1).deliver_later + }.to have_enqueued_mail(TestMailer, :email_with_args).with(1) + } + end + end end it "generates a failure message" do