ruby-on-railstestingrakeactionmailer

Rake test equivalent for ActionMailer::TestHelper?


We have recently migrated from sending emails with deliver_now to deliver_later. So that queued emails aren't lost when the system restarts, we implement this with Sidekiq.

When we used deliver_now, our Rake tests could test the sending of an email with

assert_equal 1, ActionMailer::Base.deliveries.count

For Rspec there is the assert_enqueued_emails method to test whether or not emails are queued. Is there an equivalent for Rake test?


Solution

  • The solution was quite simple, once I knew what to do:

    1. Add the line include ActionMailer::TestHelper to the start of the test file
    2. Execute the method perform_enqueued_jobs after all emails were queued.

    After that, all the old tests worked fine.

    See https://api.rubyonrails.org/classes/ActiveJob/TestHelper.html#method-i-perform_enqueued_jobs