Skip to content

Commit

Permalink
Use around/before hooks to edit verify partial doubles config
Browse files Browse the repository at this point in the history
  • Loading branch information
odlp committed Nov 4, 2024
1 parent 67a538f commit 19baec8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
25 changes: 17 additions & 8 deletions spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "rspec/rails/feature_check"
require "support/temporary_assignment"

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

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

around do |example|
original_logger = ActiveJob::Base.logger
Expand All @@ -44,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 @@ -392,10 +397,12 @@ def perform; raise StandardError; end
end

context "with partial double verification disabled" do
before do
RSpec::Mocks.configuration.verify_partial_doubles = false
end

it "skips signature checks" do
with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
expect { two_args_job.perform_later(1) }.to have_enqueued_job.with(1)
}
expect { two_args_job.perform_later(1) }.to have_enqueued_job.with(1)
end
end

Expand Down Expand Up @@ -561,12 +568,14 @@ def perform; raise StandardError; end
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)

with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
}
expect(keyword_args_job).to have_been_enqueued.with(1, 2)
end
end

Expand Down
21 changes: 13 additions & 8 deletions spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "rspec/rails/feature_check"
require "support/temporary_assignment"

if RSpec::Rails::FeatureCheck.has_active_job?
require "action_mailer"
Expand Down Expand Up @@ -50,8 +49,6 @@ def test_email; end
end

RSpec.describe "HaveEnqueuedMail matchers", skip: !RSpec::Rails::FeatureCheck.has_active_job? do
include TemporaryAssignment

before do
ActiveJob::Base.queue_adapter = :test
end
Expand All @@ -63,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 @@ -264,12 +268,13 @@ def test_email; end
end

context "with partial double verification disabled" do
before do
RSpec::Mocks.configuration.verify_partial_doubles = false
end

it "skips signature checks" do
with_temporary_assignment(RSpec::Mocks.configuration, :verify_partial_doubles, false) {
expect {
TestMailer.email_with_args(1).deliver_later
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
}
expect { TestMailer.email_with_args(1).deliver_later }
.to have_enqueued_mail(TestMailer, :email_with_args).with(1)
end
end

Expand Down
15 changes: 0 additions & 15 deletions spec/support/temporary_assignment.rb

This file was deleted.

0 comments on commit 19baec8

Please sign in to comment.