Rails 为什么我的邮件正文只有在我的测试中是空的?
In Rails why is my mail body empty only in my test?
我有一个 actionmailer class 和相关的开销,它工作得很好。然而,在我的单元测试(rails 默认最小测试)中,电子邮件正文是空的。这是为什么?
我的邮件程序 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
我的测试:
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
该邮件的浏览量不是空白,邮件中确实发送了正确的内容。那么,为什么我的测试中电子邮件正文是空白的?
您可能有两个版本的电子邮件模板(text.erb
和 html.erb
)。
如果是这样,您可以对纯文本电子邮件使用 email.text_part.body.to_s
,对 HTML 版本使用 email.html_part.body.to_s
。
我有一个 actionmailer class 和相关的开销,它工作得很好。然而,在我的单元测试(rails 默认最小测试)中,电子邮件正文是空的。这是为什么?
我的邮件程序 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
我的测试:
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
该邮件的浏览量不是空白,邮件中确实发送了正确的内容。那么,为什么我的测试中电子邮件正文是空白的?
您可能有两个版本的电子邮件模板(text.erb
和 html.erb
)。
如果是这样,您可以对纯文本电子邮件使用 email.text_part.body.to_s
,对 HTML 版本使用 email.html_part.body.to_s
。