module ActionMailer::TestHelper
def assert_emails(number, &block)
end
ContactMailer.welcome.deliver_later
ContactMailer.welcome.deliver_now
assert_emails 2 do
end
ContactMailer.welcome.deliver_now
assert_emails 1 do
def test_emails_again
emails to be sent.
If a block is passed, that block should cause the specified number of
end
assert_emails 2
ContactMailer.welcome.deliver_now
assert_emails 1
ContactMailer.welcome.deliver_now
assert_emails 0
def test_emails
Asserts that the number of emails sent matches the given number.
def assert_emails(number, &block) if block_given? diff = capture_emails(&block).length assert_equal number, diff, "#{number} emails expected, but #{diff} were sent" else assert_equal number, ActionMailer::Base.deliveries.size end end
def assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block)
end
ContactMailer.with(email: 'user@example.com').welcome.deliver_later
args: {email: 'user@example.com'} do
assert_enqueued_email_with ContactMailer, :welcome,
def test_parameterized_email
If +args+ is provided as a Hash, a parameterized email is matched.
end
end
ContactMailer.welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome do
def test_email_in_block
to be enqueued.
If a block is passed, that block should cause the specified email
end
args: ->(args) { /cheers/i.match?(args[0]) }
params: ->(params) { /hello/i.match?(params[:greeting]) },
assert_enqueued_email_with ContactMailer, :welcome,
ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
def test_email_with_matchers
end
assert_enqueued_email_with ContactMailer.with(greeting: "Hello"), :welcome
ContactMailer.with(greeting: "Hello").welcome.deliver_later
def test_email_with_parameterized_mailer
end
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: [{farewell: "Goodbye"}]
ContactMailer.with(greeting: "Hello").welcome(farewell: "Goodbye").deliver_later
def test_email_with_parameters_and_named_arguments
end
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: ["Cheers", "Goodbye"]
ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
def test_email_with_parameters_and_arguments
end
assert_enqueued_email_with ContactMailer, :welcome, args: [{ greeting: "Hello", farewell: "Goodbye" }]
ContactMailer.welcome(greeting: "Hello", farewell: "Goodbye").deliver_later
def test_email_with_named_arguments
end
assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
ContactMailer.welcome("Hello", "Goodbye").deliver_later
def test_email_with_arguments
end
assert_enqueued_email_with ContactMailer, :welcome, args: { greeting: "Hello" }
ContactMailer.with(greeting: "Hello").welcome.deliver_later
def test_email_with_parameters
end
assert_enqueued_email_with ContactMailer, :welcome
ContactMailer.welcome.deliver_later
def test_email
matching arguments and/or params.
Asserts that a specific email has been enqueued, optionally
def assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block) if mailer.is_a? ActionMailer::Parameterized::Mailer params = mailer.instance_variable_get(:@params) mailer = mailer.instance_variable_get(:@mailer) end args = Array(args) unless args.is_a?(Proc) queue ||= mailer.deliver_later_queue_name || ActiveJob::Base.default_queue_name expected = ->(job_args) do job_kwargs = job_args.extract_options! [mailer.to_s, method.to_s, "deliver_now"] == job_args && params === job_kwargs[:params] && args === job_kwargs[:args] end assert_enqueued_with(job: mailer.delivery_job, args: expected, queue: queue.to_s, &block) end
def assert_enqueued_emails(number, &block)
end
ContactMailer.welcome.deliver_later
ContactMailer.welcome.deliver_later
assert_enqueued_emails 2 do
end
ContactMailer.welcome.deliver_later
assert_enqueued_emails 1 do
def test_emails_again
emails to be enqueued.
If a block is passed, that block should cause the specified number of
end
assert_enqueued_emails 2
ContactMailer.welcome.deliver_later
assert_enqueued_emails 1
ContactMailer.welcome.deliver_later
assert_enqueued_emails 0
def test_emails
the given number.
Asserts that the number of emails enqueued for later delivery matches
def assert_enqueued_emails(number, &block) assert_enqueued_jobs(number, only: ->(job) { delivery_job_filter(job) }, &block) end
def assert_no_emails(&block)
Note: This assertion is simply a shortcut for:
end
end
# No emails should be sent from this block
assert_no_emails do
def test_emails_again
If a block is passed, that block should not cause any emails to be sent.
end
assert_emails 1
ContactMailer.welcome.deliver_now
assert_no_emails
def test_emails
Asserts that no emails have been sent.
def assert_no_emails(&block) assert_emails 0, &block end
def assert_no_enqueued_emails(&block)
end
# No emails should be enqueued from this block
assert_no_enqueued_emails do
def test_no_emails
If a block is provided, it should not cause any emails to be enqueued.
end
assert_enqueued_emails 1
ContactMailer.welcome.deliver_later
assert_no_enqueued_emails
def test_no_emails
Asserts that no emails are enqueued for later delivery.
def assert_no_enqueued_emails(&block) assert_enqueued_emails 0, &block end
def capture_emails(&block)
assert_equal "Hi there", emails.first.subject
end
ContactMailer.welcome.deliver_later
ContactMailer.welcome.deliver_now
emails = capture_emails do
assert_equal "Hi there", emails.first.subject
end
ContactMailer.welcome.deliver_now
emails = capture_emails do
def test_emails
Returns any emails that are sent in the block.
def capture_emails(&block) original_count = ActionMailer::Base.deliveries.size deliver_enqueued_emails(&block) new_count = ActionMailer::Base.deliveries.size diff = new_count - original_count ActionMailer::Base.deliveries.last(diff) end
def deliver_enqueued_emails(queue: nil, at: nil, &block)
If the +:at+ option is specified, then only delivers emails enqueued to deliver
end
assert_emails 1
end
EmployeeMailer.welcome.deliver_later # will not be performed
EmployeeMailer.deliver_later_queue_name = :internal_mailers
CustomerMailer.welcome.deliver_later # will be performed
CustomerMailer.deliver_later_queue_name = :external_mailers
deliver_enqueued_emails queue: :external_mailers do
def test_deliver_enqueued_emails_with_queue
then only the emails(s) enqueued to a specific queue will be performed.
If the +:queue+ option is specified,
end
assert_emails 1
deliver_enqueued_emails
ContactMailer.welcome.deliver_later
def test_deliver_enqueued_emails_without_block
end
assert_emails 1
end
ContactMailer.welcome.deliver_later
deliver_enqueued_emails do
def test_deliver_enqueued_emails
not given, delivers all the enqueued emails up to this point in the test.
that were enqueued throughout the duration of the block. If a block is
Delivers all enqueued emails. If a block is given, delivers all of the emails
def deliver_enqueued_emails(queue: nil, at: nil, &block) perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, queue: queue, at: at, &block) end
def delivery_job_filter(job)
def delivery_job_filter(job) job_class = job.is_a?(Hash) ? job.fetch(:job) : job.class Base.descendants.map(&:delivery_job).include?(job_class) end