Take the 2-minute tour ×

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 &raquo; Aging Reports")

This fails the test with the message:

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

I've tried things like .html_safe with no success. Is there a way to test for text containing html entities?


Here's the relevant area of the html source:

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

share|improve this question
That is likely because the HTML source contains a » and not a &raquo;. Change the HTML so that you use the &raquo; instead of ». What is the actual rendered HTML? Is the » part of the HTML source or is it defined as &raquo; in the HTML source? Please show us the source HTML. Not sure why the "expected" is showing &#187; instead of &raquo;, though. –  Zabba Oct 7 '11 at 18:18
The HTML is actually &raquo; The &#187; is another form of the &raquo; entity that I tried, but I've updated the question. –  MWean Oct 7 '11 at 18:41
It looks like RSpec is converting &raquo; into » for you. Try including the actual character in your test. –  thomasfedb Mar 18 '13 at 9:42
add comment

4 Answers

You can have the test look at the raw source of your page too:

page.body.should include(breadcrumb)

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')

This way you are explicitly looking for a href's within the breadcrumb block.

share|improve this answer
add comment

I found a workaround for the time being that uses a regex instead of a string:

find('#breadcrumbs').text.should match(/Reports . Aging Reports/)

This gets the text of the breadcrumbs div, which contains the text I'm looking for, so the scope of the match is limited. This works fine, but I'd still love to know how to match specific html entities.

share|improve this answer
add comment

html_safe does not remove html tags if thats what you want.

If you will remove html tags before you test you can use sanitize gem.

# in Gemfile
gem 'sanitize'

# in testfile
require 'sanitize'
html = Sanitize.clean(page.body.text)
html.should have_content("Reports &raquo; Aging Reports")
share|improve this answer
add comment

Sometimes the simplest solution is the right one and things just work...

page.should have_content("Reports » Aging Reports")
share|improve this answer
add comment

Your Answer


