method

assert_enqueued_email_with

Importance_3
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: ActionMailer::TestHelper
assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block) public

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

def test_email_with_matchers
  ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome,
    params: ->(params) { /hello/i.match?(params[:greeting]) },
    args: ->(args) { /cheers/i.match?(args[0]) }
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
Show source
Register or log in to add new notes.