In a Rails mailbox test, how does receive_inbound_email_from_mail differ from create_inbound_email_from_mail, and what does each exercise?
answer
- ActionMailbox::TestCase includes the helpers
- create_: a record with status processing
- receive_: create, then route at once
- _from_fixture and _from_source variants
- no route: Router::RoutingError in the test
basics
~10 screate_inbound_email_from_mail only builds and saves an InboundEmail, with status processing by default. receive_inbound_email_from_mail does the same and then routes it immediately, so ApplicationMailbox's rules, callbacks and process all run inside the test.
solid answer
~30 s`ActionMailbox::TestCase` includes `ActionMailbox::TestHelper`. `create_inbound_email_from_mail(to:, from:, subject:, body:)` builds a `Mail` message from the options or a block, saves it through `InboundEmail.create_and_extract_message_id!` with status `processing` and stops there; since the row is not `pending`, no routing job is enqueued. `receive_inbound_email_from_mail` calls the same method and then `route` synchronously, so the routing rules, the mailbox's callbacks and `process` run, and the returned record's status tells you what happened. `_from_fixture` reads a `.eml` file from `test/fixtures/files`, which is the easy way to test real attachments; `_from_source` takes a raw string. If no rule matches, `receive_*` raises `ActionMailbox::Router::RoutingError` in the test.
code
ruby · 14 linesclass ReceiptsMailboxTest < ActionMailbox::TestCase
test "known sender's receipt becomes an expense" do
assert_difference -> { users(:ana).expenses.count }, +1 do
inbound_email = receive_inbound_email_from_fixture("receipt_with_pdf.eml")
assert_predicate inbound_email, :delivered?
end
end
test "mail to an unknown address is not routed" do
assert_raises(ActionMailbox::Router::RoutingError) do
receive_inbound_email_from_mail(from: "[email protected]", to: "[email protected]")
end
end
endgo deeper
Write an ActionMailbox::TestCase that calls receive_inbound_email_from_mail and checks the result.
Explain create_ versus receive_, the processing default status, and when to use fixture or source variants.
Cover unmatched routes, failures and bounces, and test with real .eml fixtures instead of idealised hand-built messages.
Decide how much inbound-mail behaviour is pinned by tests versus monitored in production, given how varied real email clients are.
## The test case and its helpers The mailbox generator writes `test/mailboxes/receipts_mailbox_test.rb` as a subclass of **`ActionMailbox::TestCase`**, which is an `ActiveSupport::TestCase` that includes **`ActionMailbox::TestHelper`**. The helpers come in two families: | Helper | Creates an `InboundEmail` | Routes it | |---|---|---| | `create_inbound_email_from_mail(**options, &block)` | yes, status `processing` | no | | `create_inbound_email_from_fixture(name)` | yes, from a fixture file | no | | `create_inbound_email_from_source(source)` | yes, from raw text | no | | `receive_inbound_email_from_mail(**options, &block)` | yes | yes, synchronously | | `receive_inbound_email_from_fixture(name)` | yes | yes | | `receive_inbound_email_from_source(source)` | yes | yes | Every `receive_*` helper is literally its `create_*` twin followed by `.route` on the result. ## What `create_*` does `create_inbound_email_from_mail` builds a message with `Mail.new(options, &block)`, makes sure a `bcc:` option is written into the headers (Bcc is normally left out), and calls `ActionMailbox::InboundEmail.create_and_extract_message_id!` with `status: :processing`. That status is deliberate: the model's `after_create_commit :route_later` only fires for `pending` rows, so creating a test email never enqueues `ActionMailbox::RoutingJob` behind your back. Use it when you want a record to hand to code directly, for example to unit-test a mailbox: ```ruby inbound_email = create_inbound_email_from_mail(from: "[email protected]", to: "[email protected]", subject: "Taxi") ReceiptsMailbox.receive(inbound_email) ``` `ReceiptsMailbox.receive` is the class method the router itself calls, so this skips only the routing rules. ## What `receive_*` exercises `receive_inbound_email_from_mail` goes through the whole application path minus the ingress and the job: 1. `ApplicationMailbox.route` evaluates the routing rules in order; 2. the matched mailbox's `before_processing` callbacks run and may bounce; 3. `process` runs; 4. the status ends as `delivered`, `bounced` or `failed`. That makes it the right default for behaviour tests: an expense is created for a known sender, an unknown sender is bounced. Assertions usually combine a model count with the record's status: ```ruby test "unknown sender is bounced" do inbound_email = receive_inbound_email_from_mail( from: "[email protected]", to: "[email protected]", subject: "Invoice") assert_predicate inbound_email, :bounced? end ``` ## Edge cases worth a test - **No matching route.** The router marks the email bounced and raises `ActionMailbox::Router::RoutingError`, which surfaces in the test; wrap the call in `assert_raises` to pin that behaviour, or add a backstop route. - **Exceptions in `process`.** The status is set to `failed` and the exception propagates to the test unless a `rescue_from` handler on the mailbox takes it. - **Attachments and real-world formatting.** Hand-built messages rarely look like what Outlook or Gmail send. Save a real message as `test/fixtures/files/receipt_with_pdf.eml` and call `receive_inbound_email_from_fixture("receipt_with_pdf.eml")`. - **Bounce replies.** `bounce_with` enqueues an Action Mailer job; assert on it with the mailer test helpers, which are a separate topic. - **Storage.** Each test email is stored through Active Storage, so the generated `config.active_storage.service = :test` must stay in `config/environments/test.rb`. ## Manual testing in development Automated tests cover the logic; the development conductor at `/rails/conductor/action_mailbox/inbound_emails` covers the eyeball check, letting you paste a real message's source and watch it route. ## Choosing a helper - **Testing routing rules?** Use `receive_*` with only the address varying, and assert which mailbox ran by its side effect, or that `RoutingError` is raised. - **Testing one mailbox in isolation?** Use `create_*` and call `ReceiptsMailbox.receive(inbound_email)`, which skips the routing rules but runs callbacks and `process`. - **Testing parsing of real messages?** Use the `_from_fixture` variants with captured `.eml` files, one per mail client you support. - **Testing raw edge cases**, such as a message without a Message-ID? Use `_from_source` with a hand-written string; Rails generates a Message-ID from the checksum and logs a warning. Keep fixtures free of real customers' data: an `.eml` captured from production contains names, addresses and possibly the very receipts the app is meant to protect. ## What a good mailbox suite covers 1. One happy path per mailbox, through `receive_*`, asserting the domain record it creates. 2. Each `before_processing` rejection, asserting the `bounced?` status and, for `bounce_with`, that a reply was enqueued. 3. One routing test per rule, including the backstop or the `RoutingError` when there is none. 4. One test where `process` raises, asserting `failed?` or the `rescue_from` outcome you chose.
- Why does create_inbound_email_from_mail default to status processing rather than pending?`InboundEmail` enqueues `ActionMailbox::RoutingJob` in an `after_create_commit` callback only when the row is `pending`. Defaulting to `processing` means a test that just needs a record does not trigger routing in the background; tests that want routing call `receive_*`, which routes synchronously.
- How would you test a receipt with a real PDF attachment?Save a genuine message, including its MIME parts, as a `.eml` file under `test/fixtures/files`, then call `receive_inbound_email_from_fixture("receipt_with_pdf.eml")`. The helper reads it with `file_fixture`, stores it and routes it, so the mailbox sees exactly what a mail client would send.
- create_inbound_email_from_mail also runs the mailbox's process method.
- receive_inbound_email_from_mail enqueues RoutingJob and the test must perform jobs.
- Mailbox tests need a running ingress controller to receive mail.
- An unmatched address in a test silently does nothing.
- Test emails default to pending, so they route in the background.