mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add assert_enqueued_email_with to ActionMailer::TestHelper
This commit is contained in:
parent
36888b9387
commit
db6847dcb6
3 changed files with 92 additions and 0 deletions
|
@ -1,3 +1,11 @@
|
|||
* Add `assert_enqueued_email_with` test helper.
|
||||
|
||||
assert_enqueued_email_with ContactMailer, :welcome do
|
||||
ContactMailer.welcome.deliver_later
|
||||
end
|
||||
|
||||
*Mikkel Malmberg*
|
||||
|
||||
* Allow Action Mailer classes to configure their delivery job.
|
||||
|
||||
class MyMailer < ApplicationMailer
|
||||
|
|
|
@ -93,6 +93,48 @@ module ActionMailer
|
|||
assert_enqueued_jobs number, only: [ ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob ], &block
|
||||
end
|
||||
|
||||
# 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
|
||||
#
|
||||
# If `args` is provided as a Hash, a parameterized email is matched.
|
||||
#
|
||||
# def test_parameterized_email
|
||||
# assert_enqueued_email_with ContactMailer, :welcome,
|
||||
# args: {email: 'user@example.com} do
|
||||
# ContactMailer.with(email: 'user@example.com').welcome.deliver_later
|
||||
# end
|
||||
# end
|
||||
def assert_enqueued_email_with(mailer, method, args: nil, queue: "mailers", &block)
|
||||
if args.is_a? Hash
|
||||
job = ActionMailer::Parameterized::DeliveryJob
|
||||
args = [mailer.to_s, method.to_s, "deliver_now", args]
|
||||
else
|
||||
job = ActionMailer::DeliveryJob
|
||||
args = [mailer.to_s, method.to_s, "deliver_now", *args]
|
||||
end
|
||||
|
||||
assert_enqueued_with(job: job, args: args, queue: queue, &block)
|
||||
end
|
||||
|
||||
# Asserts that no emails are enqueued for later delivery.
|
||||
#
|
||||
# def test_no_emails
|
||||
|
|
|
@ -10,6 +10,18 @@ class TestHelperMailer < ActionMailer::Base
|
|||
to: "test@example.com",
|
||||
from: "tester@example.com"
|
||||
end
|
||||
|
||||
def test_args(recipient, name)
|
||||
mail body: render(inline: "Hello, #{name}"),
|
||||
to: recipient,
|
||||
from: "tester@example.com"
|
||||
end
|
||||
|
||||
def test_parameter_args
|
||||
mail body: render(inline: "All is #{params[:all]}"),
|
||||
to: "test@example.com",
|
||||
from: "tester@example.com"
|
||||
end
|
||||
end
|
||||
|
||||
class TestHelperMailerTest < ActionMailer::TestCase
|
||||
|
@ -207,6 +219,36 @@ class TestHelperMailerTest < ActionMailer::TestCase
|
|||
|
||||
assert_match(/0 .* but 1/, error.message)
|
||||
end
|
||||
|
||||
def test_assert_enqueued_email_with
|
||||
assert_nothing_raised do
|
||||
assert_enqueued_email_with TestHelperMailer, :test do
|
||||
silence_stream($stdout) do
|
||||
TestHelperMailer.test.deliver_later
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_enqueued_email_with_args
|
||||
assert_nothing_raised do
|
||||
assert_enqueued_email_with TestHelperMailer, :test_args, args: ["some_email", "some_name"] do
|
||||
silence_stream($stdout) do
|
||||
TestHelperMailer.test_args("some_email", "some_name").deliver_later
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_assert_enqueued_email_with_parameterized_args
|
||||
assert_nothing_raised do
|
||||
assert_enqueued_email_with TestHelperMailer, :test_parameter_args, args: { all: "good" } do
|
||||
silence_stream($stdout) do
|
||||
TestHelperMailer.with(all: "good").test_parameter_args.deliver_later
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AnotherTestHelperMailerTest < ActionMailer::TestCase
|
||||
|
|
Loading…
Reference in a new issue