instance method capture_emails

Ruby on Rails 7.1.6

Since v7.1.6

Available in: v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

capture_emails(&block)

Returns any emails that are sent in the block.

def test_emails
  emails = capture_emails do
    ContactMailer.welcome.deliver_now
  end
  assert_equal "Hi there", emails.first.subject

  emails = capture_emails do
    ContactMailer.welcome.deliver_now
    ContactMailer.welcome.deliver_later
  end
  assert_equal "Hi there", emails.first.subject
end

Parameters

block block
Source
# File actionmailer/lib/action_mailer/test_helper.rb, line 269
    def capture_emails(&block)
      original_count = ActionMailer::Base.deliveries.size
      deliver_enqueued_emails(&block)
      new_count = ActionMailer::Base.deliveries.size
      diff = new_count - original_count
      ActionMailer::Base.deliveries.last(diff)
    end

Defined in actionmailer/lib/action_mailer/test_helper.rb line 269 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionMailer::TestHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close