class ActionMailer::TestCase

Public Instance Methods

assert_emails(number, &block) click to toggle source

Asserts that the number of emails sent matches the given number.

def test_emails
  assert_emails 0
  ContactMailer.welcome.deliver_now
  assert_emails 1
  ContactMailer.welcome.deliver_now
  assert_emails 2
end

If a block is passed, that block should cause the specified number of emails to be sent.

def test_emails_again
  assert_emails 1 do
    ContactMailer.welcome.deliver_now
  end

  assert_emails 2 do
    ContactMailer.welcome.deliver_now
    ContactMailer.welcome.deliver_later
  end
end

See also Minitest::Rails::Expectations::ActionMailer#must_have_emails See api.rubyonrails.org/v6.1/classes/ActionMailer/TestHelper.html#method-i-assert_emails

# File lib/minitest/rails/assertions/action_mailer.rb, line 33
  
assert_enqueued_email_with(mailer, method, args: nil, queue: "mailers", &block) click to toggle source

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,
    ContactMailer.with(email: 'user@example.com').welcome.deliver_later
  end
end

See also Minitest::Rails::Expectations::ActionMailer#must_enqueue_email_with See api.rubyonrails.org/v6.1/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_email_with

# File lib/minitest/rails/assertions/action_mailer.rb, line 130
  
assert_enqueued_emails(number, &block) click to toggle source

Asserts that the number of emails enqueued for later delivery matches the given number.

def test_emails
  assert_enqueued_emails 0
  ContactMailer.welcome.deliver_later
  assert_enqueued_emails 1
  ContactMailer.welcome.deliver_later
  assert_enqueued_emails 2
end

If a block is passed, that block should cause the specified number of emails to be enqueued.

def test_emails_again
  assert_enqueued_emails 1 do
    ContactMailer.welcome.deliver_later
  end

  assert_enqueued_emails 2 do
    ContactMailer.welcome.deliver_later
    ContactMailer.welcome.deliver_later
  end
end

See also Minitest::Rails::Expectations::ActionMailer#must_have_enqueued_emails See api.rubyonrails.org/v6.1/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_emails

# File lib/minitest/rails/assertions/action_mailer.rb, line 92