2017-07-23 15:17:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 17:01:31 +00:00
|
|
|
require "active_job"
|
2015-01-08 14:41:22 +00:00
|
|
|
|
2006-11-05 21:56:18 +00:00
|
|
|
module ActionMailer
|
2014-05-26 23:38:44 +00:00
|
|
|
# Provides helper methods for testing Action Mailer, including #assert_emails
|
2015-09-28 15:47:44 +00:00
|
|
|
# and #assert_no_emails.
|
2006-11-05 21:56:18 +00:00
|
|
|
module TestHelper
|
2015-01-08 14:41:22 +00:00
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
2006-11-05 21:56:18 +00:00
|
|
|
# Asserts that the number of emails sent matches the given number.
|
|
|
|
#
|
|
|
|
# def test_emails
|
|
|
|
# assert_emails 0
|
2014-08-20 12:34:37 +00:00
|
|
|
# ContactMailer.welcome.deliver_now
|
2006-11-05 21:56:18 +00:00
|
|
|
# assert_emails 1
|
2018-03-13 23:22:07 +00:00
|
|
|
# ContactMailer.welcome.deliver_now
|
2006-11-05 21:56:18 +00:00
|
|
|
# assert_emails 2
|
|
|
|
# end
|
|
|
|
#
|
2012-09-18 00:18:56 +00:00
|
|
|
# If a block is passed, that block should cause the specified number of
|
|
|
|
# emails to be sent.
|
2006-11-05 21:56:18 +00:00
|
|
|
#
|
|
|
|
# def test_emails_again
|
|
|
|
# assert_emails 1 do
|
2014-08-20 12:34:37 +00:00
|
|
|
# ContactMailer.welcome.deliver_now
|
2006-11-05 21:56:18 +00:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# assert_emails 2 do
|
2014-08-20 12:34:37 +00:00
|
|
|
# ContactMailer.welcome.deliver_now
|
2018-03-12 18:30:25 +00:00
|
|
|
# ContactMailer.welcome.deliver_later
|
2006-11-05 21:56:18 +00:00
|
|
|
# end
|
|
|
|
# end
|
2018-03-12 18:30:25 +00:00
|
|
|
def assert_emails(number, &block)
|
2006-11-05 21:56:18 +00:00
|
|
|
if block_given?
|
|
|
|
original_count = ActionMailer::Base.deliveries.size
|
2018-11-21 22:10:18 +00:00
|
|
|
perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, &block)
|
2006-11-05 21:56:18 +00:00
|
|
|
new_count = ActionMailer::Base.deliveries.size
|
2015-05-09 09:59:59 +00:00
|
|
|
assert_equal number, new_count - original_count, "#{number} emails expected, but #{new_count - original_count} were sent"
|
2006-11-05 21:56:18 +00:00
|
|
|
else
|
2018-03-13 23:22:07 +00:00
|
|
|
assert_equal number, ActionMailer::Base.deliveries.size
|
2006-11-05 21:56:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-07 15:37:46 +00:00
|
|
|
# Asserts that no emails have been sent.
|
2006-11-05 21:56:18 +00:00
|
|
|
#
|
|
|
|
# def test_emails
|
|
|
|
# assert_no_emails
|
2014-08-20 12:34:37 +00:00
|
|
|
# ContactMailer.welcome.deliver_now
|
2006-11-05 21:56:18 +00:00
|
|
|
# assert_emails 1
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should not cause any emails to be sent.
|
|
|
|
#
|
|
|
|
# def test_emails_again
|
|
|
|
# assert_no_emails do
|
|
|
|
# # No emails should be sent from this block
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Note: This assertion is simply a shortcut for:
|
|
|
|
#
|
2018-06-29 12:58:30 +00:00
|
|
|
# assert_emails 0, &block
|
2006-11-05 21:56:18 +00:00
|
|
|
def assert_no_emails(&block)
|
|
|
|
assert_emails 0, &block
|
|
|
|
end
|
2015-01-08 14:41:22 +00:00
|
|
|
|
|
|
|
# Asserts that the number of emails enqueued for later delivery matches
|
|
|
|
# the given number.
|
|
|
|
#
|
|
|
|
# def test_emails
|
|
|
|
# assert_enqueued_emails 0
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# assert_enqueued_emails 1
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# assert_enqueued_emails 2
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should cause the specified number of
|
|
|
|
# emails to be enqueued.
|
|
|
|
#
|
|
|
|
# def test_emails_again
|
|
|
|
# assert_enqueued_emails 1 do
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# assert_enqueued_emails 2 do
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_enqueued_emails(number, &block)
|
2018-11-21 22:10:18 +00:00
|
|
|
assert_enqueued_jobs(number, only: ->(job) { delivery_job_filter(job) }, &block)
|
2015-01-08 14:41:22 +00:00
|
|
|
end
|
|
|
|
|
2017-09-24 17:55:32 +00:00
|
|
|
# Asserts that a specific email has been enqueued, optionally
|
|
|
|
# matching arguments.
|
|
|
|
#
|
|
|
|
# def test_email
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# assert_enqueued_email_with ContactMailer, :welcome
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def test_email_with_arguments
|
|
|
|
# ContactMailer.welcome("Hello", "Goodbye").deliver_later
|
|
|
|
# assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should cause the specified email
|
|
|
|
# to be enqueued.
|
|
|
|
#
|
|
|
|
# def test_email_in_block
|
|
|
|
# assert_enqueued_email_with ContactMailer, :welcome do
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2018-04-02 23:55:08 +00:00
|
|
|
# If +args+ is provided as a Hash, a parameterized email is matched.
|
2017-09-24 17:55:32 +00:00
|
|
|
#
|
|
|
|
# def test_parameterized_email
|
|
|
|
# assert_enqueued_email_with ContactMailer, :welcome,
|
2018-04-03 00:26:56 +00:00
|
|
|
# args: {email: 'user@example.com'} do
|
2017-09-24 17:55:32 +00:00
|
|
|
# ContactMailer.with(email: 'user@example.com').welcome.deliver_later
|
|
|
|
# end
|
|
|
|
# end
|
2020-12-15 10:11:58 +00:00
|
|
|
def assert_enqueued_email_with(mailer, method, args: nil, queue: ActionMailer::Base.deliver_later_queue_name || "default", &block)
|
2018-11-01 20:32:01 +00:00
|
|
|
args = if args.is_a?(Hash)
|
2018-11-30 22:33:35 +00:00
|
|
|
[mailer.to_s, method.to_s, "deliver_now", params: args, args: []]
|
2017-09-24 17:55:32 +00:00
|
|
|
else
|
2018-11-30 22:33:35 +00:00
|
|
|
[mailer.to_s, method.to_s, "deliver_now", args: Array(args)]
|
2017-09-24 17:55:32 +00:00
|
|
|
end
|
2020-12-15 10:11:58 +00:00
|
|
|
assert_enqueued_with(job: mailer.delivery_job, args: args, queue: queue.to_s, &block)
|
2017-09-24 17:55:32 +00:00
|
|
|
end
|
|
|
|
|
2015-01-08 14:41:22 +00:00
|
|
|
# Asserts that no emails are enqueued for later delivery.
|
|
|
|
#
|
|
|
|
# def test_no_emails
|
|
|
|
# assert_no_enqueued_emails
|
|
|
|
# ContactMailer.welcome.deliver_later
|
|
|
|
# assert_enqueued_emails 1
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is provided, it should not cause any emails to be enqueued.
|
|
|
|
#
|
|
|
|
# def test_no_emails
|
|
|
|
# assert_no_enqueued_emails do
|
|
|
|
# # No emails should be enqueued from this block
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_no_enqueued_emails(&block)
|
2018-11-21 22:10:18 +00:00
|
|
|
assert_enqueued_emails 0, &block
|
2015-01-08 14:41:22 +00:00
|
|
|
end
|
2018-11-21 22:10:18 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
def delivery_job_filter(job)
|
|
|
|
job_class = job.is_a?(Hash) ? job.fetch(:job) : job.class
|
|
|
|
|
2020-05-12 19:20:07 +00:00
|
|
|
Base.descendants.map(&:delivery_job).include?(job_class) ||
|
|
|
|
ActionMailer::Parameterized::DeliveryJob == job_class
|
2018-11-21 22:10:18 +00:00
|
|
|
end
|
2006-11-05 21:56:18 +00:00
|
|
|
end
|
|
|
|
end
|