instance method assert_enqueued_email_with

Ruby on Rails 7.0.10

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

assert_enqueued_email_with(mailer, method, args: nil, queue: ActionMailer::Base.deliver_later_queue_name || "default", &block)

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

Parameters

mailer req
method req
args key = nil
queue key = ActionMailer::Base.deliver_later_queue_name || "default"
block block
Source
# File actionmailer/lib/action_mailer/test_helper.rb, line 126
    def assert_enqueued_email_with(mailer, method, args: nil, queue: ActionMailer::Base.deliver_later_queue_name || "default", &block)
      args = if args.is_a?(Hash)
        [mailer.to_s, method.to_s, "deliver_now", params: args, args: []]
      else
        [mailer.to_s, method.to_s, "deliver_now", args: Array(args)]
      end
      assert_enqueued_with(job: mailer.delivery_job, args: args, queue: queue.to_s, &block)
    end

Defined in actionmailer/lib/action_mailer/test_helper.rb line 126 · 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