Skip to content

Commit

Permalink
Merge pull request #2808 from odlp/pr-suppress-job-and-mail-signature…
Browse files Browse the repository at this point in the history
…-checks

Respect verify partial doubles config when verifying job/mailer arguments
  • Loading branch information
JonRowe authored Nov 8, 2024
2 parents 1ba134d + 19baec8 commit b819663
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 27 deletions.
7 changes: 7 additions & 0 deletions lib/rspec/rails/matchers/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/matchers/have_enqueued_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
97 changes: 75 additions & 22 deletions spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 35 additions & 5 deletions spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b819663

Please sign in to comment.