使用内联附件 Rails 测试 HTML body
Testing HTML body with inline attachments Rails
我遵循文档 (http://guides.rubyonrails.org/testing.html#testing-your-mailers) 取得了一些成功。我目前正在使用固定装置(如文档所述)测试电子邮件的 body。我的电子邮件有内联附件(图片)。
我的测试(见下文)在大多数情况下都有效,尽管我 运行 遇到的问题是它将输出的 HTML 与我在夹具。因为附件是编码的,每封生成的电子邮件都有不同的图像源(测试失败,因为 HTML 不同(即)):
- Made with <img src=\"cid:56e45e159c85_13fa6229fd9889725f@775f1d813fe9.mail\" width=\"10\" height=\"10\" > in Toronto, Canada
+ Made with <img src=\"cid:56e466f93993e_13f99bbda39989734@084c0dd0f919.mail\" width=\"10\" height=\"10\" /> in Toronto, Canada
有没有办法测试 HTML body 成功?有没有办法让编码后的字符串每次都一样?
提前致谢!
require 'test_helper'
class LeadMailerTest < ActionMailer::TestCase
test "confirmation_email" do
email = LeadMailer.confirmation_email(leads(:lead), 'Varsity Heights').deliver_now
assert_not ActionMailer::Base.deliveries.empty?
assert_equal ['donotreply@yuhu.io'], email.from
assert_equal ['email2@email.com'], email.to
assert_equal 'Your request for a showing at Varsity Heights is confirmed.', email.subject
assert_equal read_fixture('confirmation_email').join, email.html_part.body.to_s
end
end
对于任何遇到这个问题的人,我最终使用 assert_select_email do,然后单独断言每个元素(并且只是图像的存在,不包括 link。
我遵循文档 (http://guides.rubyonrails.org/testing.html#testing-your-mailers) 取得了一些成功。我目前正在使用固定装置(如文档所述)测试电子邮件的 body。我的电子邮件有内联附件(图片)。
我的测试(见下文)在大多数情况下都有效,尽管我 运行 遇到的问题是它将输出的 HTML 与我在夹具。因为附件是编码的,每封生成的电子邮件都有不同的图像源(测试失败,因为 HTML 不同(即)):
- Made with <img src=\"cid:56e45e159c85_13fa6229fd9889725f@775f1d813fe9.mail\" width=\"10\" height=\"10\" > in Toronto, Canada
+ Made with <img src=\"cid:56e466f93993e_13f99bbda39989734@084c0dd0f919.mail\" width=\"10\" height=\"10\" /> in Toronto, Canada
有没有办法测试 HTML body 成功?有没有办法让编码后的字符串每次都一样?
提前致谢!
require 'test_helper'
class LeadMailerTest < ActionMailer::TestCase
test "confirmation_email" do
email = LeadMailer.confirmation_email(leads(:lead), 'Varsity Heights').deliver_now
assert_not ActionMailer::Base.deliveries.empty?
assert_equal ['donotreply@yuhu.io'], email.from
assert_equal ['email2@email.com'], email.to
assert_equal 'Your request for a showing at Varsity Heights is confirmed.', email.subject
assert_equal read_fixture('confirmation_email').join, email.html_part.body.to_s
end
end
对于任何遇到这个问题的人,我最终使用 assert_select_email do,然后单独断言每个元素(并且只是图像的存在,不包括 link。