简体   繁体   English

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

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

I am writing tests for future functionality in my ruby on rails 4 application. 我正在用Rails 4应用程序在ruby中编写有关未来功能的测试。 After reading the RoR Guides, I found I could change the smtp settings in my config/environments/test.rb file to store emails in an array and not actually send them. 阅读《 RoR指南》后,我发现可以更改config / environments / test.rb文件中的smtp设置,以将电子邮件存储在数组中而不实际发送它们。 After making that change I'm still receiving emails in my inbox when running my feature specs. 进行更改后,运行功能规格时,我仍在收件箱中接收电子邮件。

environments/test.rb 环境/ 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

In production, my emails are being sent out via the Mandrill API. 在生产中,我的电子邮件是通过Mandrill API发送的。 To double check my tests were running on the test environment, I ran: "RAILS_ENV=test rspec spec" and it still sent out emails. 为了再次检查我的测试是否在测试环境上运行,我运行:“ RAILS_ENV = test rspec spec”,它仍然发送电子邮件。

feature spec 功能规格

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

The above code fails with the following message: 上面的代码失败,并显示以下消息:

expected to find text "Welcome!" in ""

If I'm correct, it looks like the emails aren't being store in the ActionMailer::Base.deliveries array. 如果我是正确的,则似乎电子邮件没有存储在ActionMailer :: Base.deliveries数组中。

After reading more about the mandrill_mailer gem, I came across how to handle offline testing. 在阅读了有关mandrill_mailer gem的更多信息之后,我遇到了如何处理离线测试。 The issue is because I'm using mandrill's API, there is no way to intercept the email with the standard SMTP intercept method of ActionMailer. 问题是因为我使用的是mandrill的API,无法使用ActionMailer的标准SMTP拦截方法来拦截电子邮件。

In the feature spec I added: 在功能规格中,我添加了:

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

I also addded the following to spec_helper.rb 我还将以下内容添加到spec_helper.rb

require 'mandrill_mailer/offline'

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

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