In Rails why is my mail body empty only in my test?

我的梦境 提交于 2020-01-01 07:57:06

问题


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?

my mailer class:

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

my test:

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/29454102/in-rails-why-is-my-mail-body-empty-only-in-my-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!