Home How to test ActionMailer deliver_later with rspec
Reply: 0

How to test ActionMailer deliver_later with rspec

user1476 Published in June 20, 2018, 10:50 pm

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:


New controller code:


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))


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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.454935 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO