# Testing with Mechanize

**URL:** <https://www.ruby-forum.com/t/testing-with-mechanize/173265>\
**Category:** Ruby\
**Created:** [August 5, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/testing-with-mechanize/173265 "2009-08-05T23:49:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [August 5, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/testing-with-mechanize/173265/1 "2009-08-05T23:49:25Z")

</div>

Hello,

I’m trying to jump onto the habit of testing and I’m not finding to too  
easy because most of the code I write has to do with third party web  
services. My current dilemma is how to stub or mock a Mechanize object  
that deals with submitting forms. Can anyone else who tests with  
Mechanize please provide some guidance?

Thanks,  
Michael B.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [August 6, 2009, 3:20am UTC](https://www.ruby-forum.com/t/testing-with-mechanize/173265/2 "2009-08-06T03:20:34Z")

</div>

On Aug 5, 5:49 pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I’m trying to jump onto the habit of testing and I’m not finding to too  
> easy because most of the code I write has to do with third party web  
> services. My current dilemma is how to stub or mock a Mechanize object  
> that deals with submitting forms. Can anyone else who tests with  
> Mechanize please provide some guidance?

Maybe try Mechanize::Test, part of the tap-mechanize project:  
[http://tap.rubyforge.org/tap-mechanize/classes/Tap/Mechanize/Test.html](http://tap.rubyforge.org/tap-mechanize/classes/Tap/Mechanize/Test.html)

You may be able to do this with Mocha too. Perhaps someone with more  
Mocha experience can chime in.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [August 6, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/testing-with-mechanize/173265/3 "2009-08-06T15:25:14Z")

</div>

I think maybe you’re trying to test at the wrong level. Unless you’re  
actually testing the behavior of Mechanize, then I would suggest  
abstracting  
to a higher level.

Wrap your form-submission code in a method, and then mock or stub the  
method  
out with expected parameters, and perhaps return the contents of an HTML  
fixture file.

I know when I go through this sort of exercise, it forces me to think  
about  
refactoring the code to be more easily testable, which is a nice  
side-effect  
that usually improves long-term maintainability.

2009/8/5 Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [August 7, 2009, 4:49am UTC](https://www.ruby-forum.com/t/testing-with-mechanize/173265/4 "2009-08-07T04:49:12Z")

</div>

Thanks for the replies Mark and Mike. I never thought about it the way  
that you discussed Mike, and I ended up separating the form submission  
and page loading code into their own methods, which cleaned up the code  
a bit and definitely made it easier for testing. I also started using  
Mocha because I like it’s syntax more and find stubbing all instances of  
a class to be very useful. I’m glad to say I’ve finally seen the light  
of testing 🙂
