Search code examples
ruby-on-railsruby-on-rails-4rspecpostmark

ActionMailer.deliveries is always an empty array


I am using the Postmark gem for rails and writing Rspecs tests to make sure the emails are actually being sent when the form is valid.

I want to check that ActionMailer::Base.deliveries.last.to matches what I put in the contact form I'd like to test.

However, ActionMailer::Base.deliveries is always an empty array. Even in production, after an email is successfully sent (and received on the other end), the array is still empty when observed with rails c production.

What am I missing? How can I verify that the email has been sent in my tests?


Solution

  • ActionMailer::Base.deliveries array is a feature of :test delivery mode provided by ActionMailer (docs). If you’ve enabled Postmark gem in your test environment, that array will always be empty.

    Unless you’re aiming for full integration testing, you should avoid using external services in your tests and stick to the :test delivery method. If you do that, you might also want to take a look at the email-spec gem, which provides a DSL for testing emails sent with ActionMailer.

    In case you actually want to send emails in your tests, you can use Postmark Messages Retrieval API to retrieve a sent message via recipient or message ID and verify its contents. This allows for complete black-box testing if this is what you’re looking for.