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.
Asked
Active
Viewed 2,609 times
2 Answers
3
Or just strip whitespace yourself
assert_equal html_string.gsub(/\s+/, ' '), '<a href="foo">'

Alex Wayne
- 178,991
- 47
- 309
- 337
-
Just tried this regex - its not actually eating all the extra whitespace. – Joe Soul-bringer Feb 08 '09 at 01:51
-
I was thinking about it too hard. `/\s+/` seems to work though – Alex Wayne Feb 08 '09 at 02:18
-
1Stripping all whitespace is not a perfect solution because whitespace is significant inside `pre` elements, for example. – Jared Beck Dec 23 '14 at 21:40
1
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.

Alex Wayne
- 178,991
- 47
- 309
- 337
-
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 Feb 08 '09 at 00:59