The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Spy::Method - mocked method metadata

SYNOPSIS

        my $method = $spy->add_method('method_name');

        $method->should_call(sub {
                print 'called!';
        });

        ok !$method->was_called;

DESCRIPTION

This class objects let you set return values and examine call data of a specific method.

Method call result methods

The final method will return on throw whatever was last specified. Calling should_return will remove the exception throwing, if it was set up with should_throw, and vice versa.

should_return

        $method->should_return(@returns);

Sets up the return value of the method as specified in @returns.

If it consists of just one element, method will return it as scalar. Otherwise, it will return the entire @returns array as a list.

Returns $self, for chaining.

should_call

        $method->should_call(sub { ... });

Sets up the method to call the subroutine reference argument.

This subroutine will get all the regular method parameters, including $self. The subroutine should return whatever the actual method should return.

Returns $self, for chaining.

should_throw

        $method->should_throw('text exception');
        $method->should_throw($exception_object);

Instead of returning a specific value or calling a subroutine, the method can be set to always throw a given exception (a scalar value).

Returns $self, for chaining.

Call history methods

These methods are the same as documented in "Call history methods" in Test::Spy, but when called on this class objects, you don't need to setup "context" in Test::Spy.

SEE ALSO

Test::Spy