简体   繁体   English

为什么电子邮件没有进入ActionMailer :: Base.deliveries表?

[英]Why are emails not getting in the ActionMailer::Base.deliveries table?

Currently in my development environment I am not seeing anything in the ActionMailer::Base.deliveries table after mail is used. 目前在我的开发环境中,在使用mail后,我在ActionMailer::Base.deliveries表中没有看到任何内容。 Is there a setting I am missing? 我缺少一个设置吗?

config/environments/development.rb: 配置/环境/ development.rb:
config.action_mailer.delivery_method = :test . config.action_mailer.delivery_method = :test

app/mailers/notifier.rb 应用程序/邮寄者/ notifier.rb

def send_email(user)
  mail(:to => user.email, :subject => "Welcome to our website!")
end

These are the tests I'm running: 这些是我正在运行的测试:

When /^I signup$/ do
  visit signup_path
  @user = FactoryGirl.build(:user)
  fill_in "user_email", :with => @user.email
  fill_in "user_password", :with => @user.password
  click_button "Join now"
end

Then /^I should receive a confirmation email$/ do
  email = ActionMailer::Base.deliveries.last
  email.subject.should == "Welcome to our website!"
end

Right now I get the following error for the I should receive a confirmation email step: undefined method subject for nil:NilClass (NoMethodError) 现在我收到以下错误, I should receive a confirmation email步骤: undefined method subject for nil:NilClass (NoMethodError)

Thanks! 谢谢!

Another possibility might be that in config/environments/test.rb you have 另一种可能是在config / environments / test.rb中你有

config.action_mailer.delivery_method = :test

but you have set 但你已经设定好了

config.action_mailer.perform_deliveries = false

so changing to 所以改为

config.action_mailer.perform_deliveries = true

made it work again in my case. 在我的情况下让它再次起作用。

Old question, I know, but a couple things come to mind: 老问题,我知道,但有几件事情浮现在脑海中:

  • You talk about the configuration of your development environment ( config/environments/development.rb ), but it's your test that isn't working. 您谈论了开发环境的配置( config/environments/development.rb ),但这是您的测试不起作用。 Check config/environments/test.rb and maybe config\\application.rb . 检查config/environments/test.rbconfig\\application.rb

  • I actually wanted to see the email array in my development environment, but it always returned nil in the console. 我实际上想在我的开发环境中看到电子邮件阵列,但它总是在控制台中返回nil。 I finally realized that because the array is just a memory object, not in the database, the console ( rails c ) can't see what's going on in the server ( rails s ). 我终于意识到,因为数组只是一个内存对象而不是数据库,所以控制台( rails c )无法看到服务器中的内容( rails s )。 When I added <%= ActionMailer::Base.deliveries.count %> to a view, I could immediately see the array getting populated in development. 当我将<%= ActionMailer::Base.deliveries.count %>到视图中时,我可以立即看到在开发中填充了数组。

  • Another "I can't see that process from the console" situation arises if you are using a separate thread or task for sending emails. 如果您使用单独的线程或任务发送电子邮件,则会出现另一个“我无法从控制台看到该过程”的情况。 With delayed_job , I found this answer suggesting you can look at the job (not the actual email) using Delayed::Job.last.handler , which does look at the database so works across processes. 使用delayed_job ,我发现这个答案表明你可以使用Delayed::Job.last.handler查看作业(而不是实际的电子邮件),它确实查看了数据库,因此可以跨进程工作。 However this only works while the job is still in the database, ie before a worker has processed it. 但是,这仅在作业仍在数据库中时工作,即在工作人员处理之前。

The error you are getting says that email is nil. 你得到的错误说电子邮件是零。 In other words, the deliveries.last method is returning nil because there are no emails in it. 换句话说,deliveries.last方法返回nil,因为其中没有电子邮件。

There could be a number of reasons... I'm assuming that you are using cucumber as the testing mechanism? 可能有很多原因......我假设您使用黄瓜作为测试机制?

1. Verify that 'click_button' submits 1.验证“click_button”是否提交

You could try to put puts or log statements. 您可以尝试输入puts或log语句。 Make sure that when you run the cucumber feature, it actually prints. 确保在运行黄瓜功能时实际打印。 Sometimes these things don't work because the front-end logic doesn't work and the test is failing correctly. 有时这些东西不起作用,因为前端逻辑不起作用,测试失败正确。 In other words, the controller endpoint you are expecting to be triggered isn't being triggered. 换句话说,您希望被触发的控制器端点未被触发。

2. Verify that 'send_email' is right - using a unit test 2.使用单元测试验证'send_email'是否正确

Next, verify that you are actually sending email. 接下来,验证您实际上是在发送电子邮件。 Write a unit test to verify this works. 编写单元测试以验证其是否有效。 This should be quick and easy to get going. 这应该快速而容易地开始。 Plenty of articles out there. 那里有很多文章。

Lastly, not really related, but you might want to roll with email-spec which should provide some nice testing methods so you don't have to reinvent the wheel. 最后,并没有真正相关,但你可能想要使用电子邮件规范 ,这应该提供一些不错的测试方法,所以你不必重新发明轮子。

After making the changes answered by @Dominik Steiner, if it still didn't work , 在做出由@Dominik Steiner回答的更改之后,如果它仍然无效,

another possibility might be that in config/initializers/mailer.rb, you should have : 另一种可能是在config / initializers / mailer.rb中,你应该有:

ActionMailer::Base.delivery_method = :test

Above was missing in my case and it worked now. 在我的情况下,上面缺少,现在它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM