aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib
diff options
context:
space:
mode:
authorEdouard CHIN <edouard.chin@shopify.com>2018-11-21 23:10:18 +0100
committerEdouard CHIN <edouard.chin@shopify.com>2018-11-21 23:17:04 +0100
commite139a3ce1376e1b74e0996f6f0ae17e584d5acad (patch)
treee75c844b0bfd6707207470c4d18a8d2e63d5d246 /actionmailer/lib
parentcdb16ac576198607916cde6d55fe14cb775a98c9 (diff)
downloadrails-e139a3ce1376e1b74e0996f6f0ae17e584d5acad.tar.gz
rails-e139a3ce1376e1b74e0996f6f0ae17e584d5acad.tar.bz2
rails-e139a3ce1376e1b74e0996f6f0ae17e584d5acad.zip
Fix ActionMailer assertion not working for mail defining delivery_job:
- If a Mail defines a custom delivery_job, all ActionMailer assertion helper (assert_emails, assert_enqueued_emails ...) wouldn't work. ```ruby MyMailer < ApplicationMailer self.delivery_job = MyJob end # This assertion will fail assert_emails(1) do MyMailer.my_mail.deliver_later end This PR leverage the new ActiveJob feature that accepts Procs for the `only` keyword and check if the delivery job is one of ActionMailer registered ones.
Diffstat (limited to 'actionmailer/lib')
-rw-r--r--actionmailer/lib/action_mailer/test_helper.rb19
1 files changed, 14 insertions, 5 deletions
diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb
index a4751916af..b3403e8331 100644
--- a/actionmailer/lib/action_mailer/test_helper.rb
+++ b/actionmailer/lib/action_mailer/test_helper.rb
@@ -34,7 +34,7 @@ module ActionMailer
def assert_emails(number, &block)
if block_given?
original_count = ActionMailer::Base.deliveries.size
- perform_enqueued_jobs(only: [ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob], &block)
+ perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, &block)
new_count = ActionMailer::Base.deliveries.size
assert_equal number, new_count - original_count, "#{number} emails expected, but #{new_count - original_count} were sent"
else
@@ -90,7 +90,7 @@ module ActionMailer
# end
# end
def assert_enqueued_emails(number, &block)
- assert_enqueued_jobs number, only: [ ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob ], &block
+ assert_enqueued_jobs(number, only: ->(job) { delivery_job_filter(job) }, &block)
end
# Asserts that a specific email has been enqueued, optionally
@@ -125,10 +125,10 @@ module ActionMailer
# end
def assert_enqueued_email_with(mailer, method, args: nil, queue: "mailers", &block)
if args.is_a? Hash
- job = ActionMailer::Parameterized::DeliveryJob
+ job = mailer.parameterized_delivery_job
args = [mailer.to_s, method.to_s, "deliver_now", args]
else
- job = ActionMailer::DeliveryJob
+ job = mailer.delivery_job
args = [mailer.to_s, method.to_s, "deliver_now", *args]
end
@@ -151,7 +151,16 @@ module ActionMailer
# end
# end
def assert_no_enqueued_emails(&block)
- assert_no_enqueued_jobs only: [ ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob ], &block
+ assert_enqueued_emails 0, &block
end
+
+ private
+
+ def delivery_job_filter(job)
+ job_class = job.is_a?(Hash) ? job.fetch(:job) : job.class
+
+ Base.descendants.map(&:delivery_job).include?(job_class) ||
+ Base.descendants.map(&:parameterized_delivery_job).include?(job_class)
+ end
end
end