Rails - 如何测试ActionMailer在测试中发送的特定电子邮件

时间:2010-01-14 10:30:27

标签: ruby-on-rails unit-testing actionmailer

目前在我的测试中我做了类似这样的事情来测试是否有电子邮件排队等待发送

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

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

7 个答案:

答案 0 :(得分:72)

从rails 3开始,ActionMailer :: Base.deliveries是一个Mail :: Message的数组。来自mail documentation

#  mail['from'] = 'mikel@test.lindsaar.net'
#  mail[:to]    = 'you@test.lindsaar.net'
#  mail.subject 'This is a test email'
#  mail.body    = 'This is a body'
# 
#  mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...

从中可以很容易地在集成中测试您的邮件

mail = ActionMailer::Base.deliveries.last

assert_equal 'mikel@test.lindsaar.net', mail['from'].to_s

assert_equal 'you@test.lindsaar.net', mail['to'].to_s

答案 1 :(得分:27)

在测试期间使用ActionMailer时,所有邮件都放在一个名为deliveries的大数组中。你基本上在做什么(并且主要是足够的)是检查数组中是否存在电子邮件。 但是,如果您想要专门检查某个电子邮件,则必须知道数组中实际存储的内容。幸运的是,电子邮件本身已存储,因此您可以遍历数组并检查每封电子邮件。

请参阅ActionMailer::Base以查看可用的配置方法,您可以使用这些方法确定阵列中存在的电子邮件。一些最适合您案例的方法可能是

  • recipients:收取一个或多个电子邮件地址。这些地址是您的电子邮件将发送到的地方。设置To:标题。
  • subject:电子邮件的主题。设置Subject:标题。

答案 2 :(得分:18)

使用当前的Rspec语法,我最终使用了以下内容:

last_email = ActionMailer::Base.deliveries.last
expect(last_email.to).to eq ['test@example.com']
expect(last_email.subject).to have_content 'Welcome'

我的测试环境是一个功能规范,我想确保在注册后向用户发送了欢迎电子邮件。

答案 3 :(得分:3)

测试框架shoulda有一个很好的帮助器,可以让您断言发送的电子邮件的某些条件。是的,你可以用ActionMailer.deliveries自己做,但是应该把它变成一个整齐的小块

答案 4 :(得分:2)

有点晚了,但它可能对其他人有所帮助:

您可以使用Email-spec,Rspec / Minitest匹配器和Cucumber步骤的集合。

答案 5 :(得分:1)

这是我发现的最佳方式。

1)包括action mailer callbacks插件,如下所示:

script/plugin install git://github.com/AnthonyCaliendo/action_mailer_callbacks.git

我并不真正使用该插件的主要功能,但它确实提供了能够找出用于发送电子邮件的方法的很好的功能。

2)现在你可以在test_helper.rb中添加一些方法:

  def assert_sent(method_name)
    assert sent_num_times(method_name) > 0
  end

  def assert_not_sent(method_name)
    assert sent_num_times(method_name) == 0
  end

  def assert_sent_once(method_name)
    assert sent_num_times(method_name) == 1
  end

  def sent_num_times(method_name)
    count = 0
    @emails.each do |email|
      count += 1 if method_name == email.instance_variable_get("@method_name")
    end
    count
  end

3)现在你可以写下这样的甜蜜测试:

require 'test_helper'
class MailingTest < ActionController::IntegrationTest

  def setup
    @emails = ActionMailer::Base.deliveries
    @emails.clear
  end

  test "should send a mailing" do
    assert_difference "Mailing.count", 1 do
      feeds(:feed1).generate_mailing
    end

    assert_sent_once "broadcast"
    assert_not_sent "failed_mailing"
  end
end

这里“broadcast”和“mailing_failed”是我的ActionMailer :: Base类中方法的名称。这些是您通常通过拨打Mailer.deliver_broadcast(some_data)Mailer.deliver_failed_mailing(some_data)等来使用的。就是这样!

答案 6 :(得分:0)

从2020年开始(Rails 6时代,可能更早引入),您可以执行以下操作: (使用SystemTest示例)TL; DR:使用assert_emailsActionMailer::TestHelper中的ActionMailer::Base.deliveries.last访问邮件本身。

require "application_system_test_case"
require 'test_helper'
require 'action_mailer/test_helper'

class ContactTest < ApplicationSystemTestCase
  include ActionMailer::TestHelper

  test "Send mail via contact form on landing page" do
    visit root_url

    fill_in "Message", with: 'message text'

    # Asserting a mail is sent
    assert_emails 1 do
      click_on "Send"
    end

    # Asserting stuff within that mail
    last_email  = ActionMailer::Base.deliveries.last

    assert_equal ['whatever'], last_email.reply_to
    assert_equal "contact", last_email.subject
    assert_match /Mail from someone/, last_email.body.to_s
  end
end

官方文档:

注意 除了像上面的系统测试中那样手动检查邮件的内容之外,您还可以测试是否使用了特定的邮件程序操作,如下所示:

assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]

以及其他一些方便的断言,请参见https://api.rubyonrails.org/v6.0.3.2/classes/ActionMailer/TestHelper.html#method-i-assert_emails