skip to content

In a Rails integration test, how do you assert that a sign-up enqueues a welcome email, and when does assert_emails count it?

level: middleimportance: must knowfreq 50%

answer

  1. enqueued is not delivered
  2. assert_enqueued_email_with mailer, method
  3. assert_emails with a block performs deliveries
  4. ActionMailer::Base.deliveries under :test
  5. capture_emails returns the messages

basics

~20 s

Use assert_enqueued_email_with WelcomeMailer, :welcome, params: { user: user } to check the deliver_later job. assert_emails with a block performs the mail jobs enqueued inside it and counts deliveries, while assert_emails without a block only counts ActionMailer::Base.deliveries so far.

solid answer

~40 s

`deliver_later` enqueues a mail delivery job; nothing reaches `ActionMailer::Base.deliveries` until that job runs. `assert_enqueued_email_with WelcomeMailer, :welcome, params: { user: user }` (or `args:`) checks the job's mailer, method and arguments, with or without a block. `assert_enqueued_emails 1` counts mail jobs only. `assert_emails(1) { post sign_up_path, ... }` is different: with a block it calls `capture_emails`, which performs the mail jobs enqueued in the block and counts the resulting deliveries, so it passes for `deliver_later` too. Without a block, `assert_emails 1` reads `deliveries.size` at that moment, which is still 0 after `deliver_later`. `capture_emails { ... }` returns the delivered `Mail::Message` objects so you can check `to` and `subject`. Integration tests get these helpers and a cleared `deliveries` array automatically, and the generated test environment sets `delivery_method = :test`.

code

ruby · 11 lines
ruby
test "block and block-less assert_emails differ after deliver_later" do
  user = users(:mia)

  WelcomeMailer.with(user: user).welcome.deliver_later
  assert_emails 0                       # job enqueued, nothing delivered yet
  assert_enqueued_emails 1

  assert_emails 1 do                    # performs mail jobs enqueued inside
    WelcomeMailer.with(user: user).welcome.deliver_later
  end
end

go deeper

for a junior

Know that deliver_later only enqueues, and use assert_enqueued_email_with with the mailer and method for a sign-up test.

for a middle

Explain why block and block-less assert_emails differ, what capture_emails returns, and which test classes include the mail helpers.

for a senior

Choose enqueue-level or delivery-level assertions per test, and keep mail assertions specific enough that a different email cannot satisfy them.

for a principal

Decide where email behaviour is verified across mailer, integration and system tests, so templates, triggers and delivery are each covered once.

## Two stages of an email In a Rails app, sending mail in the background has two stages: 1. **Enqueue.** `WelcomeMailer.with(user: user).welcome.deliver_later` creates a mail delivery job and hands it to Active Job. Nothing is sent yet. 2. **Deliver.** When the job runs, the mailer builds the message and the configured delivery method sends it. Under the `:test` delivery method, which the generated `config/environments/test.rb` sets, "sending" means appending the message to `ActionMailer::Base.deliveries`. Rails' mail test helpers in `ActionMailer::TestHelper` target one stage or the other, and most confusion comes from mixing them up. | Helper | Stage | What it checks | |---|---|---| | `assert_enqueued_email_with(mailer, method, params:, args:)` | enqueue | a mail job for that mailer, method and arguments | | `assert_enqueued_emails(n)` / `assert_no_enqueued_emails` | enqueue | the number of mail jobs | | `assert_emails(n) { ... }` | deliver | mail delivered by the block, including jobs it enqueued | | `assert_emails(n)` without a block | deliver | `deliveries.size` right now | | `capture_emails { ... }` | deliver | returns the messages delivered in the block | | `deliver_enqueued_emails` | deliver | performs pending mail jobs | ## The welcome email in a sign-up test ```ruby class SignUpsTest < ActionDispatch::IntegrationTest test "sign-up queues the welcome email" do assert_enqueued_email_with WelcomeMailer, :welcome, params: ->(params) { params[:user].email_address == "[email protected]" } do post sign_up_path, params: { user: { email_address: "[email protected]", password: "calm-breath-42" } } end end test "the welcome email is addressed to the new user" do emails = capture_emails do post sign_up_path, params: { user: { email_address: "[email protected]", password: "calm-breath-42" } } end assert_equal ["[email protected]"], emails.first.to end end ``` `params:` and `args:` accept either values compared with `===` or a lambda, which is handy when the exact record is created inside the request and not known beforehand. ## Why assert_emails sometimes says 0 The block and block-less forms behave differently: - **With a block**, `assert_emails 1 do ... end` calls `capture_emails`, which runs the block inside `deliver_enqueued_emails`. Mail jobs enqueued by the block are performed, so `deliver_later` is counted. - **Without a block**, `assert_emails 1` simply compares with `ActionMailer::Base.deliveries.size`. After a request that only called `deliver_later`, the job has not run and the count is 0. So "the sign-up sends a welcome email" can be tested at either stage; pick the stage the test is about. Enqueue-level assertions are faster and do not render the email; delivery-level ones prove the mailer actually builds a message. ## Where the helpers are available - `ActionDispatch::IntegrationTest` gets `ActionMailer::TestHelper` and a setup and teardown that clear `deliveries`, through hooks in Action Mailer's railtie. - `ActionMailer::TestCase`, the base of mailer tests, includes them too. - `ActionMailer::TestHelper` itself includes `ActiveJob::TestHelper`, because the mail enqueue assertions are built on `assert_enqueued_with`. - A plain model test, `ActiveSupport::TestCase`, has neither; add `include ActionMailer::TestHelper` to use them there. The enqueue assertions need the Active Job test adapter, which the test environment uses unless another adapter is configured explicitly. ## Mailer tests versus integration tests The two test types answer different questions: - A **mailer test**, a subclass of `ActionMailer::TestCase`, builds the message directly with `WelcomeMailer.with(user: users(:mia)).welcome` and asserts its `to`, `subject` and body. It tests the template and the headers, not when mail is sent. - An **integration test** drives the sign-up request and asserts that the right mail was enqueued, or captures what was delivered. It tests the trigger. Keeping template checks in the mailer test means the sign-up test only needs `assert_enqueued_email_with`, which is fast and does not render anything. ## Delivering what was queued `deliver_enqueued_emails` performs pending mail delivery jobs and leaves other jobs alone. With a block, it delivers the mail enqueued inside the block; without one, it delivers everything queued so far. It accepts `queue:` and `at:` to narrow what is delivered, which helps when a test schedules a reminder for later and should only deliver the immediate welcome. ## Common mistakes - Asserting `deliveries.size` after `deliver_later` and concluding no mail was sent. - Using `assert_enqueued_jobs 1` for mail: it also counts unrelated jobs the request enqueued, while `assert_enqueued_emails` filters to mail delivery jobs. - Checking only that "an email" was enqueued, without the mailer and method, so a password-reset email would satisfy a welcome-email test.

  • Why does assert_enqueued_jobs 1 pass for the wrong reason in a sign-up test?
    It counts every Active Job job the block enqueued. If the sign-up also enqueues, say, an analytics job but no mail, the count is still 1. `assert_enqueued_emails` filters to mail delivery jobs, and `assert_enqueued_email_with` additionally checks the mailer, the method and the arguments.
  • How do you check the welcome email's subject in an integration test?
    Wrap the request in `capture_emails`, which performs the mail jobs enqueued in the block and returns the delivered `Mail::Message` objects. Then assert on `emails.first.subject` and `emails.first.to`. The content of the template itself is better covered by a mailer test.

  • deliver_later adds the message to ActionMailer::Base.deliveries immediately
  • assert_emails 1 without a block counts mail jobs that were enqueued
  • assert_enqueued_jobs 1 proves the welcome email was queued
  • A plain model test has assert_enqueued_email_with built in
  • assert_emails with a block ignores emails sent with deliver_later