0

Because we need to have the ability to schedule email delivery, we have migrated to using Sidekiq to send emails with the deliver_later method. Under the old regime that used deliver_now, our testing could use

ActionMailer::Base.deliveries[index]

to inspect the recipient, subject, body, attachments, etc...

For testing purposes, is there an equivalent mechanism to inspect the contents of queued email when using Sidekiq and deliver_later?

Edward Caulfield
  • 448
  • 5
  • 12
  • 1
    Does this answer your question? [How to test ActionMailer deliver\_later with rspec](https://stackoverflow.com/q/27647749/895789) – Alter Lagos Nov 05 '22 at 04:35
  • Thanks for the suggestion, but for me this was more complex than simply wrapping the code block in a TestHelper method. – Edward Caulfield Nov 14 '22 at 14:20

2 Answers2

0

For testing contents of the email, you have to render the email template for which the job needs to be executed. I suggest you should separate unit tests:

  1. Job spec to check if email is getting enqueued with correct parameters.
  2. Email spec to check the email contents.

If you want to go the end-to-end style, use Sidekiq::Testing.inline! to execute the job and then use ActionMailer::Base.deliveries[index] like before.

tejasbubane
  • 914
  • 1
  • 8
  • 11
0

The solution turned out to be to execute perform_enqueued_jobs after all emails were queued. After this, all of the existing testing mechanisms worked.

See https://api.rubyonrails.org/v7.0.4/classes/ActiveJob/TestHelper.html for additional information.

Edward Caulfield
  • 448
  • 5
  • 12