NAME

Test::Mock::Simple - A simple way to mock out parts of or a whole module.

SYNOPSIS

    use Test::Mock::Simple;

    my $total = 0;

    # Original::Module has methods increase, decrease, and sum
    my $mock = Test::Mock::Simple->new(module => 'Original::Module');
    $mock->add(increase => sub { shift; return $total += shift; });
    $mock->add(decrease => sub { shift; return $total -= shift; });

    my $obj = Original::Module->new();
    $obj->increase(5);
    $obj->decrease(2);
    print $obj->sum . "\n"; # prints 3

DESCRIPTION

This is a simple way of overriding any number of methods for a given object/class.

Can be used directly in test (or any) files, but best practice (IMHO) is to create a 'Mock' module and using it instead of directly using the module in any tests. The goal is to write a test which passes whether Mocking is being used or not. See TEST_MOCK_SIMPLE_DISABLE below.

The default behavior is to not allow adding methods that do not exist. This should stop mistyped method names when attempting to mock existing methods. See allow_new_methods below to change this behavior.

Why another Mock module? I needed something simple with no bells or whistles that only overrode certain methods of a given module. It's more work, but there aren't any conflicts.

This module can not do anything about BEGIN, END, or other special name code blocks. To changes these see B's (The Perl Compiler Backend) begin_av, end_av, etc. methods.

Environmental Variables

Methods

AUTHOR

Erik Tank, tank@jundy.com

COPYRIGHT AND LICENSE

Copyright (C) 2020 by Erik Tank

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.