# Parsing html files =\> putting them in fixtures for testing

**URL:** <https://www.ruby-forum.com/t/parsing-html-files-putting-them-in-fixtures-for-testing/162248>\
**Category:** Rails\
**Created:** [March 7, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/parsing-html-files-putting-them-in-fixtures-for-testing/162248 "2009-03-07T13:32:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 7, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/parsing-html-files-putting-them-in-fixtures-for-testing/162248/1 "2009-03-07T13:32:39Z")

</div>

I’m using Hpricot parser to scrape web pages. I saved two of these  
pages for a test in lack of a better way, I put the html files in the  
fixtures like this:

dl\_found\_tickets:  
html: “\<%= File.read( ‘test/fixtures/html/  
search\_dl\_found\_tickets.html’ ).gsub(’”’, ‘"’) %\>"  
[…]

Even though the crawl class works fine, the test fails, so it’s got to  
be something wrong with the fixture. I a test that compares the html  
string loaded from the fixture and the one loaded from the fixture and  
they’re not the same. From what I can tell, it’s only whitespace  
difference, from some end-of-line conversions, I guees.

This test fails:  
def test\_html\_fixtures  
assert\_equal File.read( ‘test/fixtures/html/  
search\_plate\_found\_ticket.html’ ).slice(0, 250), crawls  
(:dl\_found\_tickets).html.slice(0, 250)  
end

1. Failure:  
test\_html\_fixtures(CrawlTest)  
[test/unit/crawl\_test.rb:16:in `test_html_fixtures' /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/testing/setup_and_teardown.rb:60:in` **send** ’  
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/  
active\_support/testing/setup\_and\_teardown.rb:60:in `run’]:  
\<"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r  
\n\r\n\r\n\r\n\r\n\r\n\<link rel=“stylesheet” type=“text/  
css” h"\> expected but was  
\<" \n\n\n\n\n\n\n\n 

.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 7, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/parsing-html-files-putting-them-in-fixtures-for-testing/162248/2 "2009-03-07T14:40:02Z")

</div>

you are right. it seems like an error that occurrs after reading your  
yml-file. obviously there are some additional whitespaces/carriage  
returns added to it. but you could just gsub them.

but other than that, one question: why do you want to save a html  
string in a yml-file (and not just read your html file whenever you  
want to)?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 7, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/parsing-html-files-putting-them-in-fixtures-for-testing/162248/3 "2009-03-07T17:45:47Z")

</div>

On 7 mar, 07:39, MaD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you are right. it seems like an error that occurrs after reading your  
> yml-file. obviously there are some additional whitespaces/carriage  
> returns added to it. but you could just gsub them.

It may not be just the whitespace, because the parser gives different  
results on the YML string and the File.read string. Isn’t there a  
function to quote YML strings? I searched for it, and could not find  
it.

> but other than that, one question: why do you want to save a html  
> string in a yml-file (and not just read your html file whenever you  
> want to)?

I like it this way because I just load the object from the fixture in  
my tests.
