Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/rails/matchers/have_enqueued_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines +102 to +103
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This new spec fails without the job_match? => job_matches? rename done in this PR.)

end

it "passes when only given mailer argument" do
expect {
TestMailer.test_email.deliver_later
Expand Down Expand Up @@ -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
Comment on lines -308 to -311
Copy link
Contributor Author

@davidrunger davidrunger Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Since now there are two specs in this file (i.e. the spec being modified here, and the new spec that I added above) that have occasion to refer to a non-mailer job class, I am extracting this previously anonymous/local job class up to an actual class constant defined in the setup at the top of this file. The downside of this is that the global Ruby namespace in specs is now polluted by an additional constant, NonMailerJob, but personally I think that's worthwhile, for the upside of any specs that need to reference such a job being able conveniently to do so.)


send_time = Date.tomorrow.noon
queue = 'urgent_mail'

Expand All @@ -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)
Expand Down
Loading