Method: ActionMailer::TestHelper#assert_enqueued_email_with
- Defined in:
- actionmailer/lib/action_mailer/test_helper.rb
#assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block) ⇒ Object
Asserts that a specific email has been enqueued, optionally matching arguments and/or params.
def test_email
ContactMailer.welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome
end
def test_email_with_parameters
ContactMailer.with(greeting: "Hello").welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: { greeting: "Hello" }
end
def test_email_with_arguments
ContactMailer.welcome("Hello", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
end
def test_email_with_named_arguments
ContactMailer.welcome(greeting: "Hello", farewell: "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: [{ greeting: "Hello", farewell: "Goodbye" }]
end
def test_email_with_parameters_and_arguments
ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: ["Cheers", "Goodbye"]
end
def test_email_with_parameters_and_named_arguments
ContactMailer.with(greeting: "Hello").welcome(farewell: "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: [{farewell: "Goodbye"}]
end
def test_email_with_parameterized_mailer
ContactMailer.with(greeting: "Hello").welcome.deliver_later
assert_enqueued_email_with ContactMailer.with(greeting: "Hello"), :welcome
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: '[email protected]'} do
ContactMailer.with(email: '[email protected]').welcome.deliver_later
end
end
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'actionmailer/lib/action_mailer/test_helper.rb', line 173 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 queue ||= mailer.deliver_later_queue_name || ActiveJob::Base.default_queue_name args = if args.is_a?(Hash) [mailer.to_s, method.to_s, "deliver_now", params: args, args: []] elsif params.present? [mailer.to_s, method.to_s, "deliver_now", params: params, args: Array(args)] else [mailer.to_s, method.to_s, "deliver_now", args: Array(args)] end assert_enqueued_with(job: mailer.delivery_job, args: args, queue: queue.to_s, &block) end |