Steven Lee > Test-Magpie > Test::Magpie::Mock

Download:
Test-Magpie-0.11.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.11   Source  

NAME ^

Test::Magpie::Mock - Mock objects

SYNOPSIS ^

    # 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->any_method(@arguments);

DESCRIPTION ^

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.

ATTRIBUTES ^

class

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.

invocations

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.

stubs

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.

METHODS ^

isa

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

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

does

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

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

ref

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.

can

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');

AUTHORS ^

COPYRIGHT AND LICENSE ^

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.

syntax highlighting: