Assertion assert_enqueued_email_with with matcher support

I made a PR into Rails to add matcher support for assert_enqueued_email_with. Until that PR is merged, here’s a custom assertion assert_enqueued_email_matching that can be thrown into test_helper.rb.

# Like assert_enqueued_email_with but with args/params matching support.
# Remove once this is pulled in:
def assert_enqueued_email_matching mailer, method,
  params: nil,
  args: nil,
  queue: ActionMailer::Base.deliver_later_queue_name || "default",

  return super unless args.is_a?(Proc) || params.is_a?(Proc)

  expected_args = -> (job) {
    # Ordered from the least to the most heavy checks.
    return false if [mailer.to_s, method.to_s, "deliver_now"] != job[0..2]
    return false if !params.is_a?(Proc) && params != job[3][:params]
    return false if !args.is_a?(Proc) && Array(args) != job[3][:args]
    return false if args.is_a?(Proc) && !args.(job[3][:args])
    return false if params.is_a?(Proc) && !params.(job[3][:params])


  assert_enqueued_with \
    job: mailer.delivery_job,
    args: expected_args,
    queue: queue.to_s,

Code snippets in this post are covered by MIT License.

December 3, 2022