views:

383

answers:

2

Hi All,

I am using Ruby's Test::unit to compare the result of generated html with the expected result. (not using rails). I am not concerned with whitespace differences but these nearly always crop up during tests. Is there any testing mechanism to compare html while ignoring meaningless whitespace. I can see there's similar question for python here. I'm looking for an answer for Ruby.

+1  A: 

assert_select is what you want. It lets you use CSS selectors to parse the HTML and see if it has the right values.

See this assert_select cheat sheet

EDIT: I missed this wasn't necesarily rails. You can either import the relevant rails gem into your test environment, or use something like HPricot to allow you to prase the result as HTML and check for the right values.

Squeegy
assert_select seems to make sure that particular elements are present and I can use that elsewhere. What I would like to do is compare two whole html strings. Can it do that?
Joe Soul-bringer
+1  A: 

Or just strip whitespace yourself

assert_equal html_string.gsub(/\s+/, ' '), '<a href="foo">'
Squeegy
Just tried this regex - its not actually eating all the extra whitespace.
Joe Soul-bringer
I was thinking about it too hard. `/\s+/` seems to work though
Squeegy