diff --git a/Changelog.md b/Changelog.md index b901ab3b0..e319f865d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,11 @@ ### Development [Full Changelog](https://github.com/rspec/rspec-rails/compare/v7.0.1...main) +Bug Fixes: + +* Fix `have_enqueued_mail` wrongful failure when expecting no enqueued mail but + a non-mail job is enqueued. (David Runger, #2793) + ### 7.0.1 / 2024-09-03 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v7.0.0...v7.0.1) diff --git a/lib/rspec/rails/matchers/have_enqueued_mail.rb b/lib/rspec/rails/matchers/have_enqueued_mail.rb index 9d2c405b3..85aaea274 100644 --- a/lib/rspec/rails/matchers/have_enqueued_mail.rb +++ b/lib/rspec/rails/matchers/have_enqueued_mail.rb @@ -72,7 +72,7 @@ def mailer_class_name @mailer_class ? @mailer_class.name : 'ActionMailer::Base' end - def job_match?(job) + def job_matches?(job) legacy_mail?(job) || parameterized_mail?(job) || unified_mail?(job) end @@ -123,7 +123,7 @@ def check_active_job_adapter def unmatching_mail_jobs @unmatching_jobs.select do |job| - job_match?(job) + job_matches?(job) 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 0503080ba..14b886189 100644 --- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb +++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb @@ -20,6 +20,10 @@ class AnotherTestMailer < ActionMailer::Base def test_email; end end + class NonMailerJob < ActiveJob::Base + def perform; end + end + if RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? class UnifiedMailer < ActionMailer::Base self.delivery_job = ActionMailer::MailDeliveryJob @@ -95,6 +99,10 @@ def test_email; end expect { }.not_to have_enqueued_email end + it "passes when negated with 0 arguments and a non-mailer job is enqueued" do + expect { NonMailerJob.perform_later }.not_to have_enqueued_email + end + it "passes when only given mailer argument" do expect { TestMailer.test_email.deliver_later @@ -305,11 +313,6 @@ def test_email; end end it "generates a failure message with unmatching enqueued mail jobs" do - non_mailer_job = Class.new(ActiveJob::Base) do - def perform; end - def self.name; "NonMailerJob"; end - end - send_time = Date.tomorrow.noon queue = 'urgent_mail' @@ -320,7 +323,7 @@ def self.name; "NonMailerJob"; end expect { expect { - non_mailer_job.perform_later + NonMailerJob.perform_later TestMailer.test_email.deliver_later TestMailer.email_with_args(3, 4).deliver_later(wait_until: send_time, queue: queue) }.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)