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

标签 ruby-on-rails

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

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

应用程序/邮件程序/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)
谢谢!

最佳答案

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

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

1. 验证 'click_button' 提交

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

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

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

最后,并不是真正相关,但您可能想要使用 email-spec,它应该提供一些不错的测试方法,因此您不必重新发明轮子。

关于ruby-on-rails - 为什么电子邮件没有进入 ActionMailer::Base.deliveries 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10610150/

相关文章:

ruby-on-rails - 回形针:发现以下错误:图像的内容与报告的内容不同

ruby-on-rails - 我如何实现 Hash#all?对于多维哈希?

ruby-on-rails - 无需第二次查询即可获取 Rails 记录数

ruby-on-rails - 如何将查询转换为在 Rails 中高效执行?

ruby-on-rails - Gem 用于多表继承

ruby-on-rails - ActiveAdmin 如何添加没有模型的自定义 Controller

ruby-on-rails - Mongoid 无限循环查询

ruby-on-rails - 使用 twitter 设计/omniauth 登录 - 验证失败 : email can't be blank

ruby-on-rails - 带有请求 header 和正文的 Rails 中的 Net/HTTP

ruby-on-rails - bundler : command not found: spring