Testing with Mechanize

  • Thread starter Michael Boutros
  • Start date
M

Michael Boutros

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 Boutros
 
M

Mark Thomas

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

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

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]

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.
 
M

Michael Boutros

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top