Method: ActionMailer::TestHelper#deliver_enqueued_emails

Defined in:
actionmailer/lib/action_mailer/test_helper.rb

#deliver_enqueued_emails(queue: nil, at: nil, &block) ⇒ Object

Delivers all enqueued emails. If a block is given, delivers all of the emails that were enqueued throughout the duration of the block. If a block is not given, delivers all the enqueued emails up to this point in the test.

def test_deliver_enqueued_emails
  deliver_enqueued_emails do
    ContactMailer.welcome.deliver_later
  end

  assert_emails 1
end

def test_deliver_enqueued_emails_without_block
  ContactMailer.welcome.deliver_later

  deliver_enqueued_emails

  assert_emails 1
end

If the :queue option is specified, then only the emails(s) enqueued to a specific queue will be performed.

def test_deliver_enqueued_emails_with_queue
  deliver_enqueued_emails queue: :external_mailers do
    CustomerMailer.deliver_later_queue_name = :external_mailers
    CustomerMailer.welcome.deliver_later # will be performed
    EmployeeMailer.deliver_later_queue_name = :internal_mailers
    EmployeeMailer.welcome.deliver_later # will not be performed
  end

  assert_emails 1
end

If the :at option is specified, then only delivers emails enqueued to deliver immediately or before the given time.



247
248
249
# File 'actionmailer/lib/action_mailer/test_helper.rb', line 247

def deliver_enqueued_emails(queue: nil, at: nil, &block)
  perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, queue: queue, at: at, &block)
end