加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在rspec中的动作邮件链接检查

发布时间:2020-12-17 02:55:13 所属栏目:百科 来源:网络整理
导读:我在我的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:3
我在我的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>

如何比较邮件正文中的预期链接和编码链接?

解决方法

您可以使用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',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/)) }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读