ActionMailer在测试模式下发送真实的电子邮件

ActionMailer在测试模式下发送真实的电子邮件

本文介绍了ActionMailer在测试模式下发送真实的电子邮件! -如何关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新注册了我的小应用程序的用户必须先获得管理员(me)的批准,然后才能访问该网站。我在用户模型中使用 after_create:send_admin_email 成功生成了开发中的此类电子邮件,效果很好。我的问题是,我在测试期间(使用FactoryGirl)生成了多个用户,并且每个创建的测试用户都会发送一封真实的电子邮件。运行测试就像在一月份浇糖蜜一样,我必须删除发送到收件箱的数百封电子邮件。如何关闭它?

Newly signed up users to my little app must be approved by the admin (me) before they can gain access to the site. I've succeeded in generating such emails in development with an after_create :send_admin_email in my user model which works great. My problem is that I'm generating multiple users during my tests (using FactoryGirl) and each test user created sends off a real email. Running my tests is like pouring molasses in January and I've got to delete hundreds of emails sent to my inbox. How do I turn that off?

中的基础知识告诉我默认情况下,Action Mailer不会发送测试环境中的电子邮件。它们只是添加到ActionMailer :: Base.deliveries数组中。

Action Mailer Basics in the Rails Guides tells me that "By default Action Mailer does not send emails in the test environment. They are just added to the ActionMailer::Base.deliveries array."

此外,在 config / environments / test中.rb 我有:

config.action_mailer.delivery_method = :test

这是 config / environment.rb 的补充内容:

# Configuration for using SendGrid on Heroku
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => 'app[my app number]@heroku.com',
  :password       => '[something super secret]',
  :domain         => '[let's get this party started!.com]',
  :enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp

I'确保我缺少一些简单而基本的东西。我搜索了周围的相关问题和帖子,以探讨如何测试ActionMailer实际发送了电子邮件。

I'm sure that I'm missing something simple and basic. I've searched around and related questions and posts deal with how to test that ActionMailer actually sent email.

对于任何想法或帮助,请先多谢。

Humble gratitude in advance for any thoughts or help.

附录:在我能够停止发送电子邮件的疯狂行为。尽管如此,我还是必须将 ActionMailer :: Base.delivery_method =:test 添加到几个rspec文件中。有没有办法可以在全球范围内关闭此功能?有人对发生的事情有任何想法吗?

Addendum: Following answer to similar question found at Is it possible to turn off ActionMailer emails when cucumber testing is happening on development? I was able to stop the email sending madness. Still, I had to add ActionMailer::Base.delivery_method = :test to several rspec files. Is there a way I can shut this down globally? Anyone have any thoughts on what's going on?

推荐答案

所以我知道了。 config / environment.rb中的行 ActionMailer :: Base.delivery_method =:smtp 会覆盖 ActionMailer :: Base.delivery_method =:test 在config / environments / test.rb中。

So I've figured it out. Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb.

因此,删除该行, ActionMailer :: Base.delivery_method =:smtp'
从配置中/environment.rb,并将其放在config / environments / production.rb中。
允许您将 ActionMailer :: Base.delivery_method =:test 放在config / environments / test.rb中,并将所需的版本放在config / environments / development中.rb。当我使用Faker填充数据库并将其更改为 smtp 时,我进行了development.rb :test 的操作,因此我确定实际的电子邮件是作为额外的支票发送的。

So, 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. I made development.rb :test as I populated my database using Faker and changed it to smtp so I was sure that real emails were sent as an additional check.

注意:您必须重新启动服务器才能使这些更改生效。

Note: You must restart your server for these changes to take effect.

另一个说明:将SendGrid Heroku配置代码放入新的config / initializers / mail.rb文件中,这可能需要删除其最后一行并将所需的版本放在每个config / environments / [production.rb,development.rb,test中。 rb]

Another note: Heroku's current SendGrid Instructions put the SendGrid Heroku configuration code in a new config/initializers/mail.rb file which will likely require removing its last line and placing the desired version in each config/environments/[production.rb, development.rb, test.rb]

这篇关于ActionMailer在测试模式下发送真实的电子邮件! -如何关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:35