问题描述
我正在编写一个请求规范,并想测试字符串"Reports»Aging Reports"的存在.如果直接在匹配表达式中输入字符,则会收到错误消息(无效的多字节字符),因此我尝试了以下操作:page.should have_content("Reports » Aging Reports")
I'm writing a request spec and would like to test for the presence of the string "Reports » Aging Reports". I get an error (invalid multibyte char) if I put in the character in my matcher expression directly, so I tried this: page.should have_content("Reports » Aging Reports")
这将失败,并显示以下消息:
This fails the test with the message:
expected there to be content "Reports » Aging Reports" in "\n Reports » Aging Reports\n
我尝试了类似.html_safe
之类的方法,但均未成功.有没有一种方法可以测试包含html实体的文本?
I've tried things like .html_safe
with no success. Is there a way to test for text containing html entities?
以下是html源代码的相关区域:
Here's the relevant area of the html source:
<a href="/reports">Reports</a> » <a href="/aging_reports">Aging Reports</a>
推荐答案
您也可以对页面的原始源进行测试:
You can have the test look at the raw source of your page too:
breadcrumb = '<a href="/reports">Reports</a> » <a href="/aging_reports">Aging Reports</a>'
page.body.should include(breadcrumb)
expect(page.body).to include(breadcrumb) # rspec 2.11+
话虽如此,我不确定这是否是最优雅的解决方案.假设您的链接周围有一个名为.breadcrumb
的类,您可以验证div中是否存在这些链接:
With that said I'm not sure that's the most elegant solution. Assuming there is a class named .breadcrumb
around your links you could just validate the links are present within the 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
通过这种方式,您可以在面包屑块中明确查找href.
This way you are explicitly looking for a href's within the breadcrumb block.
这篇关于Rspec测试页面内容中的html实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!