在Rails中,为什么仅在测试中我的邮件正文为空? [英] In Rails why is my mail body empty only in my test?

查看:100
本文介绍了在Rails中,为什么仅在测试中我的邮件正文为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个actionmailer类和相关的开销,它运行完美.但是,在我的单元测试(rails默认为minitest)中,电子邮件正文为空.为什么会这样?

I have an actionmailer class and associated overhead, it works perfectly. In my unit test (rails default minitest) however, the email body is empty. Why is that?

我的邮件班级:

class TermsMailer < ApplicationMailer
  default from: "info@domain.com"
  def notice_email(user,filename)
    @user = user
    @file = filename
    mail(to: "info@domain.com", subject: 'Data downloaded')
  end
end

我的测试:

require 'test_helper'

class TermsMailerTest < ActionMailer::TestCase
  test "notice" do
    email = TermsMailer.notice_email(users(:me),'file.ext').deliver_now
    assert_not ActionMailer::Base.deliveries.empty?
    assert_equal ['info@domain.com'], email.from
    assert_equal ['info@domain.com'], email.to
    assert_equal 'Data downloaded', email.subject
    assert_equal 'arbitrary garbage for comparison', email.body.to_s
  end
end

此邮件的视图不是空白,并且实际上已在电子邮件中发送了正确的内容.那么,为什么我的测试中的电子邮件正文为空白?

The views for this mailer are not blank, and the correct contents are in fact sent in the emails. So why is the email body blank in my test?

推荐答案

您可能有两个版本的电子邮件模板(text.erbhtml.erb).

You probably have two versions of the email templates (text.erb and html.erb).

如果是这样,可以将email.text_part.body.to_s用于纯文本电子邮件,将email.html_part.body.to_s用于HTML版本.

If so, you can use email.text_part.body.to_s for plain-text email and email.html_part.body.to_s for HTML version.

这篇关于在Rails中,为什么仅在测试中我的邮件正文为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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