1

I am using rails 6.01, and delayed_job, 4.1.8. I have set the active job queue adapter

#config/environments/test.rb
config.active_job.queue_adapter = :inline

The rspec code is

before(:each) { ActionMailer::Base.deliveries = [] }
it 'sends some message' do
      UserMailer.delay.some_message
      mail = ActionMailer::Base.deliveries  
      expect(mail.count).to eq 1
end

This test fails with a count of 0. Looking at the log the mail message is queued into DelayedJob but not executed:

Delayed::Backend::ActiveRecord::Job Create (0.4ms)  INSERT INTO "delayed_jobs" ("handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["handler", "--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class 'UserMailer'\nmethod_name: :some_message"], ["run_at", "2019-12-12 18:29:26.457639"], ["created_at", "2019-12-12 18:29:26.458062"], ["updated_at", "2019-12-12 18:29:26.458062"]]

f I using binding.pry in the test and examine Rails.application.config.queue_adapter.queue the value is #<ActiveJob::QueueAdapters::InlineAdapter:0x00007faefad1d408> which seems correct, so this problem is probably not caused by this rails 6 issue.

If I use

UserMailer.some_message.deliver

the test passes, so there is nothing wrong with the mailer.

How do I fix this?

Obromios
  • 15,408
  • 15
  • 72
  • 127
  • Does this answer your question? [Rails 6 & deliver\_later doesn't affect ActionMailer::Base.deliveries](https://stackoverflow.com/questions/57776074/rails-6-deliver-later-doesnt-affect-actionmailerbase-deliveries) – Andres Leon Jan 09 '23 at 14:33

2 Answers2

0

As a work around, the following in the Rspec config block worked

  config.before do
      Delayed::Worker.delay_jobs = false
  end

However, I would still appreciate an answer to the question.

Obromios
  • 15,408
  • 15
  • 72
  • 127
0

I encountered a similar problem and found that Rails 6 seems to be invoking ActiveJob::TestHelper::TestQueueAdapter (https://api.rubyonrails.org/files/activejob/lib/active_job/test_helper_rb.html) as the default queue adapter during tests. So when you send an email with .deliver_later in a test case, you can check if the mail was queued using assert_enqueued_jobs, then you can actually perform the UserMailer job with perform_enqueued_jobs, then you'll be able to access the actual mail from ActionMailer::Base.deliveries.