From 19baec8921527cb3f240946dfd8d3d84997e9cbb Mon Sep 17 00:00:00 2001 From: Oli Peate Date: Mon, 4 Nov 2024 10:00:54 +0000 Subject: [PATCH] Use around/before hooks to edit verify partial doubles config https://github.com/rspec/rspec-rails/pull/2808#pullrequestreview-2411993274 --- spec/rspec/rails/matchers/active_job_spec.rb | 25 +++++++++++++------ .../rails/matchers/have_enqueued_mail_spec.rb | 21 ++++++++++------ spec/support/temporary_assignment.rb | 15 ----------- 3 files changed, 30 insertions(+), 31 deletions(-) delete mode 100644 spec/support/temporary_assignment.rb diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 54f73de28..5b299a28b 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb index bc5da9b46..d2cc57ba4 100644 --- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb +++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb @@ -1,5 +1,4 @@ require "rspec/rails/feature_check" -require "support/temporary_assignment" if RSpec::Rails::FeatureCheck.has_active_job? require "action_mailer" @@ -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 @@ -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 { @@ -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 diff --git a/spec/support/temporary_assignment.rb b/spec/support/temporary_assignment.rb deleted file mode 100644 index d07aadcad..000000000 --- a/spec/support/temporary_assignment.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module TemporaryAssignment - def with_temporary_assignment(assignee, attribute, temporary_value) - predicate = "#{attribute}?" - attribute_reader = assignee.respond_to?(predicate) ? predicate : attribute - attribute_writer = "#{attribute}=" - - original_value = assignee.public_send(attribute_reader) - assignee.public_send(attribute_writer, temporary_value) - yield - ensure - assignee.public_send(attribute_writer, original_value) - end -end