我在我的rspec测试中有这样的比较:
expect(new_mail.body.encoded).to match(url_for(controller:'some_controller',action:'some_action',some_param:some_param))
但它失败了,因为ActionMailer将html主体编码为类似的东西:
+<a href=3D"http://localhost:3000/some_controller/some_action/wgt1p468ersmkq=
+gbvmfv5wgmifj13u894dfjrhc0hzczk71pcw3hrk5907iqfolc6onhxvik6apuuwgm1rng7ro=
+rt8qih43thql3spyp5ajjdugy9jvx0xc5hvpi015z" style=3D"display: inline-block=
+; background-color: #71B344; color: #FFF; border-radius: 4px; font-weight=
+: bold; text-decoration: none; padding: 6px 12px; white-space: nowrap">
+ Go to Your Account
+ </a>
如何比较邮件正文中的预期链接和编码链接?
答案 0 :(得分:5)
您可以使用new_mail.body.to_s
来获取Guide to Rails Testing中指示的非编码版本。
此外,您可以使用Capybara在呈现的电子邮件(或any html fragment)上进行断言,如果您确实想要确保链接在那里,那么这比使用字符串匹配更好:
let(:email){ Capybara::Node::Simple.new(new_mail.body.to_s) }
it "has a link" do
url = url_for(controller:'some_controller',action:'some_action',some_param:some_param)
expect(email).to have_link('A link', href: url)
end
<强>加了:强>
从多部分电子邮件中获取正确的部分:
def get_message_part(mail, content_type)
mail.body.parts.find { |p| p.content_type.match content_type }.body.raw_source
end
let(:email) { Capybara::Node::Simple.new(get_message_part(new_mail, /html/)) }