instance method assert_enqueued_email_with

Ruby on Rails 7.1.6

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, params: nil, args: nil, queue: nil, &block)

Asserts that a specific email has been enqueued, optionally matching arguments and/or params.

def test_email
  ContactMailer.welcome.deliver_later
  assert_enqueued_email_with ContactMailer, :welcome
end

def test_email_with_parameters
  ContactMailer.with(greeting: "Hello").welcome.deliver_later
  assert_enqueued_email_with ContactMailer, :welcome, args: { greeting: "Hello" }
end

def test_email_with_arguments
  ContactMailer.welcome("Hello", "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
end

def test_email_with_named_arguments
  ContactMailer.welcome(greeting: "Hello", farewell: "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome, args: [{ greeting: "Hello", farewell: "Goodbye" }]
end

def test_email_with_parameters_and_arguments
  ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: ["Cheers", "Goodbye"]
end

def test_email_with_parameters_and_named_arguments
  ContactMailer.with(greeting: "Hello").welcome(farewell: "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome, params: { greeting: "Hello" }, args: [{farewell: "Goodbye"}]
end

def test_email_with_parameterized_mailer
  ContactMailer.with(greeting: "Hello").welcome.deliver_later
  assert_enqueued_email_with ContactMailer.with(greeting: "Hello"), :welcome
end

def test_email_with_matchers
  ContactMailer.with(greeting: "Hello").welcome("Cheers", "Goodbye").deliver_later
  assert_enqueued_email_with ContactMailer, :welcome,
    params: ->(params) { /hello/i.match?(params[:greeting]) },
    args: ->(args) { /cheers/i.match?(args[0]) }
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
params key = nil
args key = nil
queue key = nil
block block
Source
# File actionmailer/lib/action_mailer/test_helper.rb, line 157
    def assert_enqueued_email_with(mailer, method, params: nil, args: nil, queue: nil, &block)
      if mailer.is_a? ActionMailer::Parameterized::Mailer
        params = mailer.instance_variable_get(:@params)
        mailer = mailer.instance_variable_get(:@mailer)
      end

      if args.is_a?(Hash)
        ActionMailer.deprecator.warn <<~MSG
          Passing a Hash to the assert_enqueued_email_with :args kwarg causes the
          Hash to be treated as params. This behavior is deprecated and will be
          removed in Rails 7.2.

          To specify a params Hash, use the :params kwarg:

            assert_enqueued_email_with MyMailer, :my_method, params: { my_param: "value" }

          Or, to specify named mailer args as a Hash, wrap the Hash in an array:

            assert_enqueued_email_with MyMailer, :my_method, args: [{ my_arg: "value" }]
            # OR
            assert_enqueued_email_with MyMailer, :my_method, args: [my_arg: "value"]
        MSG

        params, args = args, nil
      end

      args = Array(args) unless args.is_a?(Proc)
      queue ||= mailer.deliver_later_queue_name || ActiveJob::Base.default_queue_name

      expected = ->(job_args) do
        job_kwargs = job_args.extract_options!

        [mailer.to_s, method.to_s, "deliver_now"] == job_args &&
          params === job_kwargs[:params] && args === job_kwargs[:args]
      end

      assert_enqueued_with(job: mailer.delivery_job, args: expected, queue: queue.to_s, &block)
    end

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