module ActionMailer::TestHelper
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