# Unit testing offline with mechanize

**URL:** <https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473>\
**Category:** Ruby\
**Created:** [September 13, 2008, 7:46am UTC](https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473 "2008-09-13T07:46:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 13, 2008, 7:46am UTC](https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473/1 "2008-09-13T07:46:40Z")

</div>

Although mechanize is designed to navigate around web pages online, is  
there a way to load up webpages saved to the harddrive for the purposes  
of quick unit testing. Constantly accessing online sources slows down  
the tests. I tried to simply type the file name instead of the url in  
agent.get("…") but it complains. I tried get\_file and that didnt work  
either.

any help greatly appreciated.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 13, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473/2 "2008-09-13T16:46:07Z")

</div>

Adam A. wrote:

> Although mechanize is designed to navigate around web pages online, is  
> there a way to load up webpages saved to the harddrive for the purposes  
> of quick unit testing. Constantly accessing online sources slows down  
> the tests. I tried to simply type the file name instead of the url in  
> agent.get("…") but it complains. I tried get\_file and that didnt work  
> either.

That sounds like the assumption you must serve a page through a web  
server just  
to unit test it.

Why not do what Rails tests do - call the same method a server would  
call to  
generate a page, then read it as a string and test it?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 14, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473/3 "2008-09-14T15:37:45Z")

</div>

thanks philip for the reply. Im new to ruby and havnt experimented with  
rails yet etc though i think i understand what you mean.

The reason why i want mechanize to fetch the offline page rather than  
say reading it in as a string via IO.read(“offlinepage.htm”) is because  
in teh past ive noticed that the html code in a page saved from firefox  
is sligthly different from that say of open-uri due to the fact that  
firefox cleans up dodgy html (to an extent). this is also true with  
firebug - what it shows in its console may not actually be present in  
the actual html downloaded with open-uri.  
I dont know if this is the case with mechanize. As i said im new to ruby  
and web programming so i really dont know how to work round these  
differences apart from just using one tool to grab the pages and then  
open them.

Am i wrong on this? Id really like to know how people get round this  
problem.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 14, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/unit-testing-offline-with-mechanize/148473/4 "2008-09-14T18:33:21Z")

</div>

a tip would be to save the page with mechanize , read it then locally  
within a script , get your regexes ( or whatever you’re doing to parse  
the data ) right , and then try them on the online version .
