繁体   English   中英

使用Rspec测试时,将电子邮件存储在ActionMailer :: Base.deliveries数组中

[英]Store emails in ActionMailer::Base.deliveries array when testing with Rspec

我正在用Rails 4应用程序在ruby中编写有关未来功能的测试。 阅读《 RoR指南》后,我发现可以更改config / environments / test.rb文件中的smtp设置,以将电子邮件存储在数组中而不实际发送它们。 进行更改后,运行功能规格时,我仍在收件箱中接收电子邮件。

环境/ test.rb

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

在生产中,我的电子邮件是通过Mandrill API发送的。 为了再次检查我的测试是否在测试环境上运行,我运行:“ RAILS_ENV = test rspec spec”,它仍然发送电子邮件。

功能规格

# Excerpt of feature spec
email = ActionMailer::Base.deliveries.last
email.body.raw_source.should include "Welcome!"

上面的代码失败,并显示以下消息:

expected to find text "Welcome!" in ""

如果我是正确的,则似乎电子邮件没有存储在ActionMailer :: Base.deliveries数组中。

在阅读了有关mandrill_mailer gem的更多信息之后,我遇到了如何处理离线测试。 问题是因为我使用的是mandrill的API,无法使用ActionMailer的标准SMTP拦截方法来拦截电子邮件。

在功能规格中,我添加了:

email = MandrillMailer::deliveries.last
expect(email).to_not be_nil

我还将以下内容添加到spec_helper.rb

require 'mandrill_mailer/offline'

暂无
暂无

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

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