3

Everytime I run tests, emails are actually sent. My config/environments/test.rb has this configuration

config.action_mailer.delivery_method = :test

So I thought that whenever I run test, they should not be really sent, but when I run the command

RAILS_ENV="test" rake test

The emails get sent either way. For a moment I thought that maybe I was not using the right environment. But then I deleted completely db/test.sqlite3 and immediately after I run the tests again. The file was again restored, which proves that I am actually running in the test environment.

What is going on? Why are my tests sending real mails? Could it have anything to do with the fact that I am using deliver_now on my app?

Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189

3 Answers3

4

You should at least be able to disable them by placing

config.action_mailer.perform_deliveries = false

in your environments/test.rb as suggested by Brent Sullivan in his answer.

Community
  • 1
  • 1
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
2

Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb.

delete that line, ActionMailer::Base.delivery_method = :smtp' from config/environment.rb and place it in config/environments/production.rb. That allows you to place ActionMailer::Base.delivery_method = :test in config/environments/test.rb and the version you want in config/environments/development.rb.

Shiva
  • 11,485
  • 2
  • 67
  • 84
DJSampat
  • 309
  • 2
  • 8
0

This is for Rails 6 but I thought it might help someone who is having this problem.

make sure to put the following in your config/environments/test.rb file:

  config.action_mailer.delivery_method = :test
  config.active_job.queue_adapter = :test
chell
  • 7,646
  • 16
  • 74
  • 140