Rspec测试页面内容中的html实体

MWe*_*ean 12 ruby rspec ruby-on-rails html-entities

我正在编写请求规范,并希望测试字符串"Reports»Aging Reports"的存在.如果我直接在我的matcher表达式中输入字符,我会收到一个错误(无效的多字节字符),所以我尝试了这个:page.should have_content("Reports » Aging Reports")

这使测试失败并显示以下消息:

expected there to be content "Reports » Aging Reports" in "\n Reports » Aging Reports\n

我尝试过.html_safe没有成功的事情.有没有办法测试包含html实体的文本?

编辑:

这是html源代码的相关区域:

<a href="/reports">Reports</a> &raquo; <a href="/aging_reports">Aging Reports</a>

har*_*low 7

您也可以通过测试查看页面的原始来源:

breadcrumb = '<a href="/reports">Reports</a> &raquo; <a href="/aging_reports">Aging Reports</a>'
page.body.should include(breadcrumb)
expect(page.body).to include(breadcrumb) # rspec 2.11+
Run Code Online (Sandbox Code Playgroud)

话虽如此,我不确定这是最优雅的解决方案.假设.breadcrumb您的链接周围有一个类,您可以只验证div中存在的链接:

within '.breadcrumb' do
  page.should have_css('a', text: 'Reports')
  page.should have_css('a', text: 'Aging Reports')
  expect(page).to have_css('a', text: 'Reports') # rspec 2.11+
  expect(page).to have_css('a', text: 'Aging Reports') # rspec 2.11+
end
Run Code Online (Sandbox Code Playgroud)

这样你就可以在breadcrumb块中明确地寻找一个href.


dei*_*vid 5

有时,最简单的解决方案就是正确的解决方案,并且一切正常......

\n\n
page.should have_content("Reports \xc2\xbb Aging Reports")\n
Run Code Online (Sandbox Code Playgroud)\n


MWe*_*ean 1

我暂时找到了一个解决方法,使用正则表达式而不是字符串:

find('#breadcrumbs').text.should match(/Reports . Aging Reports/)
Run Code Online (Sandbox Code Playgroud)

这样获取的是div的文本breadcrumbs,其中包含了我要查找的文本,所以匹配的范围是有限的。这工作正常,但我仍然想知道如何匹配特定的 html 实体。