How to test ActionMailer deliver_later with rspec

trying to upgrade to Rails 4.2, using delayed_job_active_record. I've not set the delayed_job backend for test environment as thought that way jobs would execute straight away.

I'm trying to test the new 'deliver_later' method with RSpec, but I'm not sure how.

Old controller code:

ServiceMailer.delay.new_user(@user)

New controller code:

ServiceMailer.new_user(@user).deliver_later

I USED to test it like so:

expect(ServiceMailer).to receive(:new_user).with(@user).and_return(double("mailer", :deliver => true))

Now I get errors using that. (Double "mailer" received unexpected message :deliver_later with (no args))

Just

expect(ServiceMailer).to receive(:new_user)

fails too with 'undefined method `deliver_later' for nil:NilClass'

I've tried some examples that allow you to see if jobs are enqueued using test_helper in ActiveJob but I haven't managed to test that the correct job is queued.

expect(enqueued_jobs.size).to eq(1)

This passes if the test_helper is included, but it doesn't allow me to check it is the correct email that is being sent.

What I want to do is:

  • test that the correct email is queued (or executed straight away in test env)
  • with the correct parameters (@user)

Any ideas?? thanks


If I understand you correctly, you could do:

message_delivery = instance_double(ActionMailer::MessageDelivery)
expect(ServiceMailer).to receive(:new_user).with(@user).and_return(message_delivery)
allow(message_delivery).to receive(:deliver_later)

The key thing is that you need to somehow provide a double for deliver_later.


Using ActiveJob and rspec-rails 3.4+, you could use have_enqueued_job like this:

expect { 
  YourMailer.your_method.deliver_later 
  # or any other method that eventually would trigger mail enqueuing
}.to( 
  have_enqueued_job.on_queue('mailers').with(
    # `with` isn't mandatory, but it will help if you want to make sure is
    # the correct enqueued mail.
    'YourMailer', 'your_method', 'deliver_now', any_param_you_want_to_check
  )
)

also double check in config/environments/test.rb you have:

config.action_mailer.delivery_method = :test
config.active_job.queue_adapter = :test

Another option would be to run inline jobs:

config.active_job.queue_adapter = :inline

But keep in mind this would affect the overall performance of your test suite, as all your jobs will run as soon as they're enqueued.


If you find this question but are using ActiveJob rather than simply DelayedJob on its own, and are using Rails 5, I recommend configuring ActionMailer in config/environments/test.rb:

config.active_job.queue_adapter = :inline

(this was the default behavior prior to Rails 5)


I will add my answer because none of the others was good enough for me:

1) There is no need to mock the Mailer: Rails basically does that already for you.

2) There is no need to really trigger the creation of the email: this will consume time and slow down your test!

That's why in environments/test.rb you should have the following options set:

config.action_mailer.delivery_method = :test
config.active_job.queue_adapter = :test

Again: don't deliver your emails using deliver_now but always use deliver_later. That prevents your users from waiting for the effective delivering of the email. If you don't have sidekiq, sucker_punch, or any other in production, simply use config.active_job.queue_adapter = :async. And either async or inline for development environment.

Given the following configuration for the testing environment, you emails will always be enqueued and never executed for delivery: this prevents your from mocking them and you can check that they are enqueued correctly.

In you tests, always split the test in two: 1) One unit test to check that the email is enqueued correctly and with the correct parameters 2) One unit test for the mail to check that the subject, sender, receiver and content are correct.

Given the following scenario:

class User
  after_update :send_email

  def send_email
    ReportMailer.update_mail(id).deliver_later
  end
end

Write a test to check the email is enqueued correctly:

include ActiveJob::TestHelper
expect { user.update(name: 'Hello') }.to have_enqueued_job(ActionMailer::DeliveryJob).with('ReportMailer', 'update_mail', 'deliver_now', user.id)

and write a separate test for your email

Rspec.describe ReportMailer do
    describe '#update_email' do
      subject(:mailer) { described_class.update_email(user.id) }
      it { expect(mailer.subject).to eq 'whatever' }
      ...
    end
end
  • You have tested exactly that your email has been enqueued and not a generic job.
  • Your test is fast
  • You needed no mocking

When you write a system test, feel free to decide if you want to really deliver emails there, since speed doesn't matter that much anymore. I personally like to configure the following:

RSpec.configure do |config|
  config.around(:each, :mailer) do |example|
    perform_enqueued_jobs do
      example.run
    end
  end
end

and assign the :mailer attribute to the tests were I want to actually send emails.

For more about how to correctly configure your email in Rails read this article: https://medium.com/@coorasse/the-correct-emails-configuration-in-rails-c1d8418c0bfd