CONTENTS

NAME

Test::MockObject - Perl extension for emulating troublesome interfaces

SYNOPSIS

use Test::MockObject;
my $mock = Test::MockObject->new();
$mock->set_true( 'somemethod' );
ok( $mock->somemethod() );

$mock->set_true( 'veritas')
     ->set_false( 'ficta' )
     ->set_series( 'amicae', 'Sunny', 'Kylie', 'Bella' );

DESCRIPTION

It's a simple program that doesn't use any other modules, and those are easy to test. More often, testing a program completely means faking up input to another module, trying to coax the right output from something you're not supposed to be testing anyway.

Testing is a lot easier when you can control the entire environment. With Test::MockObject, you can get a lot closer.

Test::MockObject allows you to create objects that conform to particular interfaces with very little code. You don't have to reimplement the behavior, just the input and the output.

IMPORTANT CAVEATS

Before you go wild with your testing powers, consider three caveats:

EXPORT

None.

METHODS

The most important thing a Mock Object can do is to conform sufficiently to an interface. For example, if you're testing something that relies on CGI.pm, you may find it easier to create a mock object that returns controllable results at given times than to fake query string input.

The Basics

Mocking

Your mock object is nearly useless if you don't tell it what it's mocking. This is done by installing methods. You control the output of these mocked methods. In addition, any mocked method is tracked. You can tell not only what was called, but which arguments were passed. Please note that you cannot track non-mocked method calls. They will still be allowed, though Test::MockObject will carp() about them. This is considered a feature, though it may be possible to disable this in the future.

As implied in the example above, it's possible to chain these calls together. Thanks to a suggestion from the fabulous Piers Cawley (CPAN RT #1249), this feature came about in version 0.09. Shorter testing code is nice!

Checking Your Mocks

Logging

Test::MockObject logs all mocked methods by default. Sometimes you don't want to do this. To prevent logging all calls to a given method, prepend the name of the method with - when mocking it.

That is:

$mock->set_true( '-foo', 'bar' );

will set mock both foo() and bar(), causing both to return true. However, the object will log only calls to bar(), not foo(). To log foo() again, merely mock it again without the leading -:

$mock->set_true( 'foo' );

$mock will log all subsequent calls to foo() again.

Subclassing

There are two methods provided for subclassing:

TODO

AUTHOR

chromatic, <chromatic at wgz dot org>

Thanks go to Curtis 'Ovid' Poe, as well as ONSITE! Technology, Inc., for finding several bugs and providing several constructive suggestions.

Jay Bonci also found a false positive in called_ok(). Thanks!

Chris Winters was the first to report I'd accidentally scheduled 0.12 for deletion without uploading a newer version. He also gave useful feedback on Test::MockObject::Extends.

Stevan Little provided the impetus and code for set_isa().

Nicholas Clark found a documentation error.

Mutant suggested a potential problem with fake_module().

SEE ALSO

perl, Test::Tutorial, Test::More, http://www.perl.com/pub/a/2001/12/04/testing.html, and http://www.perl.com/pub/a/2002/07/10/tmo.html.

COPYRIGHT

Copyright (c) 2002 - 2016 by chromatic <chromatic at wgz dot org>.

This program is free software; you can use, modify, and redistribute it under the same terms as Perl 5.24 itself.

See http://www.perl.com/perl/misc/Artistic.html