Rails-如何测试ActionMailer在测试中发送了特定的电子邮件 [英] Rails - How do you test ActionMailer sent a specific email in tests

查看:86
本文介绍了Rails-如何测试ActionMailer在测试中发送了特定的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前在我的测试中,我会执行类似的操作来测试电子邮件是否已排队发送

Currently in my tests I do something like this to test if an email is queued to be sent

assert_difference('ActionMailer::Base.deliveries.size', 1) do       
  get :create_from_spreedly, {:user_id => @logged_in_user.id}
end

但是如果ia控制器动作可以发送两个不同的电子邮件,即如果注册正常,则向用户发送一个通知,如果出现问题,则向管理员发送通知-我该如何测试实际发送的是哪个。上面的代码无论如何都可以通过。

but if i a controller action can send two different emails i.e. one to the user if sign up goes fine or a notification to admin if something went wrong - how can i test which one actually got sent. The code above would pass regardless.

推荐答案

在测试过程中使用ActionMailer时,所有邮件都放在一个名为<$的大数组中。 c $ c>交货。基本上,您正在做的工作(并且通常就足够了)是检查数组中是否存在电子邮件。
但是,如果要专门检查某个电子邮件,则必须知道数组中实际存储的内容。幸运的是,电子邮件本身已存储,因此您可以遍历数组并检查每封电子邮件。

When using the ActionMailer during tests, all mails are put in a big array called deliveries. What you basically are doing (and is sufficient mostly) is checking if emails are present in the array. But if you want to specifically check for a certain email, you have to know what is actually stored in the array. Luckily the emails themselves are stored, thus you are able to iterate through the array and check each email.

请参见 ActionMailer :: Base 来查看可用的配置方法,您可以用来确定阵列中存在哪些电子邮件。一些最适合您的情况的方法可能是

See ActionMailer::Base to see what configuration methods are available, which you can use to determine what emails are present in the array. Some of the most suitable methods for your case probably are


  • 收件人:需要一个或更多电子邮件地址。这些地址是您的电子邮件将发送到的地址。设置收件人:标题。

  • 主题:电子邮件的主题。设置Subject:标题。

  • recipients: Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the To: header.
  • subject: The subject of your email. Sets the Subject: header.

这篇关于Rails-如何测试ActionMailer在测试中发送了特定的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆