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::MonkeyMock

SYNOPSIS

    # Create a new mock object
    my $mock = Test::MonkeyMock->new;
    $mock->MOCK(foo => sub {'bar'});
    $mock->foo;

    # Mock existing object
    my $mock = Test::MonkeyMock->new(MyObject->new());
    $mock->MOCK(foo => sub {'bar'});
    $mock->foo;

    # Check how many times the method was called
    my $count = $mock->CALLED('foo');

    # Check what arguments were passed on the first call
    my @args = $mock->CALL_ARGS('foo');

    # Check what arguments were passed on the second call
    my @args = $mock->CALL_ARGS('foo', 1);

DESCRIPTION

Why? I used and still use Test::MockObject and Test::MockObject::Extends a lot but sometimes it behaves very strangely introducing hard to find global bugs in the test code, which is very painful, since the test suite should have as least bugs as possible. Test::MonkeyMock is somewhat a subset of Test::MockObject but without side effects.

Test::MonkeyMock is also very strict. When mocking a new object:

  • throw when using CALLED on unmocked method

  • throw when using CALL_ARGS on unmocked method

When mocking an existing object:

  • throw when using MOCK on unknown method

  • throw when using CALLED on unknown method

  • throw when using CALL_ARGS on unknown method

AUTHOR

Viacheslav Tykhanovskyi, vti@cpan.org.

COPYRIGHT AND LICENSE

Copyright (C) 2012, Viacheslav Tykhanovskyi

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.