Rails指导测试邮件程序的示例?

时间:2014-02-23 20:18:28

标签: ruby-on-rails ruby ruby-on-rails-4

用于测试邮件程序的rails指南示例代码如下:

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase
  tests UserMailer
  test "invite" do
    # Send the email, then test that it got queued
    email = UserMailer.create_invite('me@example.com',
                                     'friend@example.com', Time.now).deliver
    assert !ActionMailer::Base.deliveries.empty?

    # Test the body of the sent email contains what we expect it to
    assert_equal ['me@example.com'], email.from
    assert_equal ['friend@example.com'], email.to
    assert_equal 'You have been invited by me@example.com', email.subject
    assert_equal read_fixture('invite').join, email.body.to_s
  end
end

这条线的目的是什么,我必须在测试中添加它吗? - &GT; 测试UserMailer

1 个答案:

答案 0 :(得分:1)

请查看tests method implementation

以下carlosantoniodasilvatests

的说法
  

您可以在邮件程序/控制器/帮助程序测试中使用测试来定义   您正在测试哪个邮件程序/控制器/帮助程序,以防它不能   自动从测试名称中猜到。例如,UserMailerTest   自动猜测UserMailer作为测试主题,但你可以   明确说出或说它与测试的名称不同   方法。因此,拥有这条线并非完全错误,只是   明确Rails自动做什么。

您可以在Github

阅读有关此特定主题的完整讨论