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