The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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 CAVEAT FOR TESTERS

Please note that it is possible to write highly detailed unit tests that pass even when your integration tests may fail. Testing the pieces individually does not excuse you from testing the whole thing together. I consider this to be a feature.

EXPORT

None by default. Maybe the Test::Builder accessories, in a future version.

FUNCTIONS

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

  • new

    Creates a new mock object. Currently, this is a blessed hash. In the future, there may be support for different types of objects.

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!k

  • mock(name, coderef)

    Adds a coderef to the object. This allows the named method to be called on the object. For example, this code:

            my $mock = Test::MockObject->new();
            $mock->mock('fluorinate', 
                    sub { 'impurifying precious bodily fluids' });
            print $mock->fluorinate;

    will print a helpful warning message. Please note that methods are only added to a single object at a time and not the class. (There is no small similarity to the Self programming language, or the Class::Prototyped module.)

    This method forms the basis for most of Test::MockObject's testing goodness.

    Please Note: this method used to be called add(). Due to its ambiguity, it is now spelled differently. For backwards compatibility purposes, add() is available, though deprecated as of version 0.07. It goes to some contortions to try to do what you mean, but I make few guarantees.

  • fake_module(module name), [ subname = coderef, ... ]

    Lies to Perl that a named module has already been loaded. This is handy when providing a mockup of a real module if you'd like to prevent the actual module from interfering with the nice fakery. If you're mocking Regexp::English, say:

            $mock->fake_module( 'Regexp::English' );

    This can be invoked both as a class and as an object method. Beware that this must take place before the actual module has a chance to load. Either wrap it in a BEGIN block before a use or require, or place it before a use_ok() or require_ok() call.

    You can optionally add functions to the mocked module by passing them as name => coderef pairs to fake_module(). This is handy if you want to test an import():

            my $import;
            $mock->fake_module(
                    'Regexp::English',
                    import => sub { $import = caller }
            );
            use_ok( 'Regexp::Esperanto' );
            is( $import, 'Regexp::Esperanto',
                    'Regexp::Esperanto should use() Regexp::English' );
  • fake_new(module name)

    Provides a fake constructor for the given module that returns the invoking mock object. Used in conjunction with fake_module(), you can force the tested unit to work with the mock object instead.

            $mock->fake_module( 'CGI' );
            $mock->fake_new( 'CGI' );
    
            use_ok( 'Some::Module' );
            my $s = Some::Module->new();
            is( $s->{_cgi}, $mock,
                    'new() should create and store a new CGI object' );
  • set_always(name, value)

    Adds a method of the specified name that always returns the specified value.

  • set_true(name)

    Adds a method of the specified name that always returns a true value.

  • set_false(name)

    Adds a method of the specified name that always returns a false value. (Since it installs an empty subroutine, the value should be false in both scalar and list contexts.)

  • set_list(name, [ item1, item2, ... ]

    Adds a method that always returns a given list of values. It takes some care to provide a list and not an array, if that's important to you.

  • set_series(name, [ item1, item2, ... ]

    Adds a method that will return the next item in a series on each call. This can be an effective way to test error handling, by forcing a failure on the first method call and then subsequent successes. Note that the series is (eventually) destroyed.

  • set_bound(name, reference)

    Adds a method bound to a variable. Pass in a reference to a variable in your test. When you change the variable, the return value of the new method will change as well. This is often handier than replacing mock methods.

  • remove(name)

    Removes a named method.

Checking Your Mocks

  • called(name)

    Checks to see if a named method has been called on the object. This returns a boolean value. The current implementation does not scale especially well, so use this sparingly if you need to search through hundreds of calls.

  • clear()

    Clears the internal record of all method calls on the object. It's handy to do this every now and then.

  • next_call([ position ])

    Returns the name and argument list of the next mocked method that was called on an object, in list context. In scalar context, returns only the method name. There are two important things to know about this method. First, it starts at the beginning of the call list. If your code runs like this:

            $mock->set_true( 'foo' );
            $mock->set_true( 'bar' );
            $mock->set_true( 'baz' );
    
            $mock->foo();
            $mock->bar( 3, 4 );
            $mock->foo( 1, 2 );

    Then you might get output of:

            my ($name, $args) = $mock->next_call();
            print "$name (@$args)";
    
            # prints 'foo'
    
            $name = $mock->next_call();
            print $name;
    
            # prints 'bar'
    
            ($name, $args) = $mock->next_call();
            print "$name (@$args)";
    
            # prints 'foo 1 2'

    If you provide an optional number as the position argument, the method will skip that many calls, returning the data for the last one skipped.

            $mock->foo();
            $mock->bar();
            $mock->baz();
    
            $name = $mock->next_call();
            print $name;
    
            # prints 'foo'
    
            $name = $mock->next_call( 2 );
            print $name
    
            # prints 'baz'

    When it reaches the end of the list, it returns undef. This is probably the most convenient method in the whole module, but for the sake of completeness and backwards compatibility (it takes me a while to reach the truest state of laziness!), there are several other methods.

  • call_pos(position)

    Returns the name of the method called on the object at a specified position. This is handy if you need to test a certain order of calls. For example:

            Some::Function( $mock );
            is( $mock->call_pos(1),  'setup',
                    'Function() should first call setup()' );
            is( $mock->call_pos(-1), 'end', 
                    '... and last call end()' );

    Positions can be positive or negative. Please note that the first position is, in fact, 1. (This may change in the future. I like it, but am willing to reconsider.)

  • call_args(position)

    Returns a list of the arguments provided to the method called at the appropriate position. Following the test above, one might say:

            is( ($mock->call_args(1))[0], $mock,
                    '... passing the object to setup()' );
            is( scalar $mock->call_args(-1), 0,
                    '... and no args to end()' );
  • call_args_pos(call position, argument position)

    Returns the argument at the specified position for the method call at the specified position. One might rewrite the first test of the last example as:

            is( $mock->call_args_pos(1, 1), $mock,
                    '... passing the object to setup()');
  • call_args_string(position, [ separator ])

    Returns a stringified version of the arguments at the specified position. If no separator is given, they will not be separated. This can be used as:

            is( $mock->call_args_string(1), "$mock initialize",
                    '... passing object, initialize as arguments' );
  • called_ok(method name, [ test name ])

    Tests to see whether a method of the specified name has been called on the object. This and the following methods use Test::Builder, so they integrate nicely with a test suite built around Test::Simple, Test::More, or anything else compatible:

            $mock->foo();
            $mock->called_ok( 'foo' );

    A generic default test name is provided.

  • called_pos_ok(position, method name, [ test name ])

    Tests to see whether the named method was called at the specified position. A default test name is provided.

  • called_args_pos_is(method position, argument position, expected, [ test name ])

    Tests to see whether the argument at the appropriate position of the method in the specified position equals a specified value. A default, rather non-descript test name is provided.

  • called_args_string_is(method position, separator, expected, [ test name ])

    Joins together all of the arguments to a method at the appropriate position and matches against a specified string. A generically bland test name is provided by default. You can probably do much better.

TODO

  • Write an article about how to use this and why :) (very soon)

  • Add a factory method to avoid namespace collisions (soon)

  • Handle isa()

  • Make fake_module() and fake_new() undoable

  • Allow different types of blessed referents

  • Add more useful methods (catch import()?)

AUTHOR

chromatic, <chromatic@wgz.org>

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

SEE ALSO

perl, Test::Tutorial, Test::More, http://www.perl.com/pub/a/2001/12/04/testing.html.

COPYRIGHT

Copyright 2002 by chromatic <chromatic@wgz.org>.

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

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