Test::Magpie::Mock - Mock objects


    # create a mock object
    my $mock = mock(); # from Test::Magpie
    my $mock_with_class = mock('AnyRef');

    # mock objects pretend to be anything you want them to be
    $true = $mock->isa('AnyClass');
    $true = $mock->does('AnyRole');
    $true = $mock->DOES('AnyRole');
    $ref  = ref($mock_with_class); # AnyRef

    # call any method with any arguments
    $method_ref = $mock->can('any_method');


Mock objects are the objects you pass around as if they were real objects. They do not have a defined API; any method may be called. Additionally, you can create stubs to specify responses (return values or exceptions) to method calls.

A mock objects records every method called on it along with their arguments. These records may then be used for verifying that the correct interactions occured.



The name of the class that the object is pretending to be blessed into. Calling ref() on the mock object will return this class name.


An array reference containing a record of all methods invoked on this mock. These are used for verification and inspection.

This attribute is internal, and not publically accessible.


Contains all of the methods stubbed for this mock. It maps the method name to an array of stubs. Stubs are matched against invocation arguments to determine which stub to dispatch to.

This attribute is internal, and not publically accessible.



Always returns true. It allows the mock object to isa() any class that is required.

    $true = $mock->isa('AnyClass');


Always returns true. It allows the mock object to does() any role that is required.

    $true = $mock->does('AnyRole');
    $true = $mock->DOES('AnyRole');


Returns the object's class attribute value. This also works if you call ref() as a function instead of a method.

    $mock  = mock('AnyRef');
    $class = $mock->ref;  # or ref($mock)

If the object's class attribute has not been set, then it will fallback to returning the name of this class.


Always returns a reference to the AUTOLOAD() method. It allows the mock object to can() do any method that is required.

    $method_ref = $mock->can('any_method');


  • Oliver Charles <>

  • Steven Lee <>


This software is copyright (c) 2013 by Oliver Charles.

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