【问题标题】:Why are emails not getting in the ActionMailer::Base.deliveries table?为什么邮件没有进入 ActionMailer::Base.deliveries 表?
【发布时间】:2012-05-23 12:29:33
【问题描述】:

目前在我的开发环境中,使用mail 后,我在ActionMailer::Base.deliveries 表中看不到任何内容。有没有我遗漏的设置?

config/environments/development.rb:
config.action_mailer.delivery_method = :test.

app/mailers/notifier.rb

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

这些是我正在运行的测试:

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

现在,I should receive a confirmation email 步骤出现以下错误: undefined method subject for nil:NilClass (NoMethodError)

谢谢!

【问题讨论】:

  • 您对您的邮件有看法吗?你是怎么测试的?

标签: ruby-on-rails


【解决方案1】:

另一种可能性可能是在 config/environments/test.rb 你有

config.action_mailer.delivery_method = :test

但是你已经设置了

config.action_mailer.perform_deliveries = false

所以改成

config.action_mailer.perform_deliveries = true

在我的情况下让它再次工作。

【讨论】:

    【解决方案2】:

    老问题,我知道,但我想到了几件事:

    • 您谈论的是您的开发环境的配置 (config/environments/development.rb),但您的测试不起作用。检查config/environments/test.rb,也许config\application.rb

    • 我实际上想在我的开发环境中查看电子邮件数组,但它总是在控制台中返回 nil。我终于意识到,因为数组只是一个内存对象,不在数据库中,所以控制台(rails c)无法看到服务器(rails s)中发生了什么。当我将<%= ActionMailer::Base.deliveries.count %> 添加到视图中时,我可以立即看到该数组正在开发中填充。

    • 如果您使用单独的线程或任务发送电子邮件,则会出现另一种“我无法从控制台看到该进程”的情况。通过delayed_job,我发现this answer 建议您可以使用Delayed::Job.last.handler 查看工作(不是实际的电子邮件),确实查看数据库,因此可以跨进程工作。但是,这只适用于作业仍在数据库中时,即在工作人员处理它之前。

    【讨论】:

      【解决方案3】:

      您收到的错误表明电子邮件为零。换句话说,deliveries.last 方法返回 nil,因为其中没有电子邮件。

      可能有很多原因...我假设您使用黄瓜作为测试机制?

      1.验证“click_button”是否提交

      您可以尝试放置 puts 或 log 语句。确保在运行 cucumber 功能时,它会实际打印。有时这些事情不起作用,因为前端逻辑不起作用并且测试正确失败。换句话说,您期望被触发的控制器端点没有被触发。

      2。验证“send_email”是否正确 - 使用单元测试

      接下来,验证您是否确实在发送电子邮件。编写一个单元测试来验证它是否有效。这应该快速且容易上手。那里有很多文章。

      最后,不是很相关,但您可能想使用email-spec,它应该提供一些不错的测试方法,这样您就不必重新发明轮子了。

      【讨论】:

        【解决方案4】:

        在进行@Dominik Steiner 回答的更改后,如果仍然无法正常工作,

        另一种可能是在 config/initializers/mailer.rb 中,你应该有:

        ActionMailer::Base.delivery_method = :test
        

        以上在我的情况下丢失了,它现在可以工作了。

        【讨论】:

          猜你喜欢
          • 2012-04-15
          • 2011-04-29
          • 1970-01-01
          • 2020-01-06
          • 2022-01-17
          • 2011-08-16
          • 2012-07-06
          • 2022-12-18
          • 1970-01-01
          相关资源
          最近更新 更多