Test::MockObject? What is that? How do I use? Why??? [Was: Devel::Cover recommendations...]
David Cantrell
david at cantrell.org.uk
Mon Mar 19 16:59:24 GMT 2007
On Mon, Mar 19, 2007 at 09:40:12AM +0100, Luis Motta Campos wrote:
> On Mar 17, 2007, at 9:40 PM, Adrian Howard wrote:
> >use Test::MockObject;
> >my $mock = Test::MockObject->new->
> > mock( randommethod => sub { print "i like pie\n" } );
> >$mock->fake_module( 'MockedModule' );
> >$mock->fake_new( 'MockedModule' );
> >MockedModule->new->randommethod;
> Please don't laught, but I can't see any good reason to use this
> on a test.
I wouldn't want to do a test that printed "i like pie". I reduced the
problem to the smallest possible example of the problem so that the
people helping me didn't have to fight through the specifics of my
situation.
> Mr. Cantrell, could you please tell me how are you using this and
> why?
I have a module which fetches data from the interwebnets and
transmogrifies it. Trouble is, I can't reliably test everything it does
against the live servers. I need to test with known data (as opposed to
whatever data they're spitting out at the moment) so that I can make
certain that my transmogrifications are correct. I need to test both
success and two different types of failure - and remote failures are
outside my control. And finally, one of the sites I want to test
against rations the amount of data any host can fetch from it in a given
amount of time. I want to avoid hitting that limit purely because when
*I* am fixing my code and running the tests over and over again I'll very
quickly hit that limit.
So I'm mocking some functions out of LWP::Request and its friends and
relations. That way, I can set a couple of variables in the test suite
to make it look like the website returns a known chunk of data, or I can
set those variables to make it look like the website is failing in
particular ways. That way, I can test *all* the possible types of input
to my code.
The module in question is Net::Random. See here in CVS:
http://shorterlink.org/1893
in particular the tests I run in lines 49 to 57.
No, those tests aren't finished.
--
David Cantrell | Nth greatest programmer in the world
Fashion label: n: a liferaft for personalities
which lack intrinsic buoyancy
More information about the london.pm
mailing list