Improvements to assert_enqueued_email_with

We’ve just submitted a PR to hopefully improve the assert_enqueued_email_with method. This makes the passing of parameterized mailer parameters and mailer method arguments explicit. It contains some other goodness and updated docs. I would love some feedback!

Args

TestHelperMailer.test_args("some_email", "some_name").deliver_later

matches

assert_enqueued_email_with TestHelperMailer, :test_args, args: ["some_email", "some_name"]

Params

TestHelperMailer.with(all: "good").test_parameter_args.deliver_later

matches

assert_enqueued_email_with TestHelperMailer, :test_parameter_args, args: { all: "good" }

Named Args

TestHelperMailer.test_named_args(email: "some_email", name: "some_name").deliver_later

matches

assert_enqueued_email_with TestHelperMailer, :test_named_args, args: [{ email: "some_email", name: "some_name" }]

Params and Args

TestHelperMailer.with(all: "good").test_args("some_email", "some_name").deliver_later

matches

assert_enqueued_email_with TestHelperMailer, :test_args, params: { all: "good" }, args: ["some_email", "some_name"]

Parameterized Mailer

TestHelperMailer.with(all: "good").test_parameter_args.deliver_later

matches

assert_enqueued_email_with TestHelperMailer.with(all: "good"), :test_parameter_args

That last scenario is questionable, but I think beautiful and readable.

What is your use case for making this explicit?

The biggest issue is that you can’t currently use this assertion when you have arguments for a parameterized mailer.

For example:

class UserMailer < ActionMailer::Base
  def invoice(invoice)
    @user = params[:user]
    ...
  end
end

There is no way to make this assertion match because it assumes the args is either mailer parameters or mailer method arguments:

  assert_enqueued_email_with UserMailer, :invoice, args: [ invoice, params: { user: user }]
1 Like