Deliver later not working in the test environment in Rails 5
Asked Answered
D

2

4

Basically, with the test config set up exactly how it worked in Rails 4 (delivery method set as test etc), aside from deprecated options which I have replaced, mail only sends with deliver_now, not deliver_later. Deliver_later works in the development environment, even when the config is identical between the two environments.

Test environment mailer config:

config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = true
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Dumbarton answered 24/9, 2016 at 14:45 Comment(2)
have you tried to set active_job.queue_adapter?Partnership
from test.rb comment: The :test delivery method accumulates sent emails in the ActionMailer::Base.deliveries array. so why do you want them to be sent?Partnership
M
7

I have the same issue, and i have instead resorted to using assertions on

enqueued_jobs.size

to assert my mail has been queued. Additionally i have unit tests on the mailer class where i use deliver_now.

This requires including ActiveJob:TestHelper

class ActiveSupport::TestCase
  include ActiveJob::TestHelper
end
Machuca answered 16/12, 2016 at 8:51 Comment(1)
Thanks a bunch. That project is on the back burner and the rails 5 branch got stale but when I get round to revisiting it this should get everything passing.Dumbarton
C
1

Another option for testing emails that use deliver_later (Active Job) is to put the code you want executed now (not queued) and your assertions in a perform_enqueued_jobs block. This also requires adding an include at the top of your test file just below the class definition

include ActiveJob::TestHelper

Then something like the below.

perform_enqueued_jobs do
  post article_url, params: { article: { title: "Learn Testing", body: "Lorem Ipsum" } }
  assert_not_equal 0, ActionMailer::Base.deliveries.size
end
Crick answered 3/10, 2017 at 23:49 Comment(1)
Wouldn't this actually attempt to send the mails?Formularize

© 2022 - 2024 — McMap. All rights reserved.