【发布时间】:2012-06-13 06:55:16
【问题描述】:
新注册我的小应用程序的用户必须得到管理员(我)的批准才能访问该网站。我已经成功地在我的用户模型中使用after_create :send_admin_email 在开发中生成了这样的电子邮件,效果很好。我的问题是我在测试期间生成了多个用户(使用 FactoryGirl),并且创建的每个测试用户都会发送一封真实的电子邮件。运行我的测试就像在一月份倒糖蜜,我必须删除发送到我收件箱的数百封电子邮件。如何关闭它?
Rails Guides 中的Action Mailer Basics 告诉我“默认情况下,Action Mailer 不会在测试环境中发送电子邮件。它们只是添加到 ActionMailer::Base.deliveries 数组中。”
此外,在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
我确定我缺少一些简单而基本的东西。我已经四处搜索,相关的问题和帖子涉及如何测试 ActionMailer 实际发送的电子邮件。
提前对任何想法或帮助表示谦卑的感谢。
附录:在Is it possible to turn off ActionMailer emails when cucumber testing is happening on development? 找到类似问题的答案后,我能够阻止电子邮件发送疯狂。尽管如此,我还是不得不将ActionMailer::Base.delivery_method = :test 添加到几个rspec 文件中。有没有办法可以在全球范围内关闭它?有人对发生的事情有任何想法吗?
【问题讨论】:
-
通过所有这些电子邮件,我超过了 Send Grid 允许的每天 200 封邮件。我发现How to write features that don't actually use Sendgrid? 表示
ActionMailer::Base.delivery_method = :smtp' in 'config/environment.rb在config/environments/test.rb中覆盖config.action_mailer.delivery_method = :test我已经移动了ActionMailer::Base.delivery_method = :smtp' intoconfig/environments/development.rb` 和config/environments/production.rb,这可能会起作用。明天会更新。
标签: ruby-on-rails-3 actionmailer rspec-rails sendgrid