Home > OS >  Testing Mails queued with Sidekiq
Testing Mails queued with Sidekiq

Time:11-15

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?

CodePudding user response:

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.

CodePudding user response:

The solution turned out to be to wrap the existing code in perform_enqueued_jobs. After this, all of the existing testing mechanisms worked.

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

  • Related