module Minitest::Rails::Expectations::ActionMailer

Public Instance Methods

must_enqueue_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
  must_enqueue_email_with ContactMailer, :welcome
end

def test_email_with_arguments
  ContactMailer.welcome("Hello", "Goodbye").deliver_later
  must_enqueue_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
  must_enqueue_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
  must_enqueue_email_with ContactMailer, :welcome,
    ContactMailer.with(email: 'user@example.com').welcome.deliver_later
  end
end

See also ActionMailer::TestClass#assert_enqueued_email_with See api.rubyonrails.org/v6.0/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_email_with

# File lib/minitest/rails/expectations/action_mailer.rb, line 134
        
must_have_emails(number, &block) click to toggle source

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

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

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

def test_emails_again
  must_have_emails 1 do
    ContactMailer.welcome.deliver_now
  end

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

See also ActionMailer::TestClass#assert_emails See api.rubyonrails.org/v6.0/classes/ActionMailer/TestHelper.html#method-i-assert_emails

# File lib/minitest/rails/expectations/action_mailer.rb, line 38
        
must_have_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
  must_have_enqueued_emails 0
  ContactMailer.welcome.deliver_later
  must_have_enqueued_emails 1
  ContactMailer.welcome.deliver_later
  must_have_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
  must_have_enqueued_emails 1 do
    ContactMailer.welcome.deliver_later
  end

  must_have_enqueued_emails 2 do
    ContactMailer.welcome.deliver_later
    ContactMailer.welcome.deliver_later
  end
end

See also ActionMailer::TestClass#assert_enqueued_emails See api.rubyonrails.org/v6.0/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_emails

# File lib/minitest/rails/expectations/action_mailer.rb, line 96
        
wont_have_emails(&block) click to toggle source

Asserts that no emails have been sent.

def test_emails
  wont_have_emails
  ContactMailer.welcome.deliver_now
  must_have_emails 1
end

If a block is passed, that block should not cause any emails to be sent.

def test_emails_again
  wont_have_emails do
    # No emails should be sent from this block
  end
end

Note: This assertion is simply a shortcut for:

must_have_emails 0, &block

See also ActionMailer::TestClass#wont_have_emails See api.rubyonrails.org/v6.0/classes/ActionMailer/TestHelper.html#method-i-assert_no_emails

# File lib/minitest/rails/expectations/action_mailer.rb, line 64
        
wont_have_enqueued_emails(&block) click to toggle source

Asserts that no emails are enqueued for later delivery.

def test_no_emails
  wont_have_enqueued_emails
  ContactMailer.welcome.deliver_later
  wont_have_enqueued_emails 1
end

If a block is provided, it should not cause any emails to be enqueued.

def test_no_emails
  wont_have_enqueued_emails do
    # No emails should be enqueued from this block
  end
end

See also ActionMailer::TestClass#assert_no_enqueued_emails See api.rubyonrails.org/v6.0/classes/ActionMailer/TestHelper.html#method-i-assert_no_enqueued_emails

# File lib/minitest/rails/expectations/action_mailer.rb, line 157
extend ::ActiveSupport::Concern