1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package EasyMocker;
# I want an easy to use mocker, with pretty explicit syntax
use strict;
use warnings;
use vars qw(@EXPORT);
use base 'Exporter';
@EXPORT = qw(mock should method);
# syntax:
# use t::lib::EasyMocker;
# mock 'My::Class::method' => with sub { };
# or even
# mock 'My::Class', 'method' => with sub { };
sub method { @_ }
sub should { @_ }
my $MOCKS = {};
sub mock {
{
no strict 'refs';
no warnings 'redefine', 'prototype';
if (@_ == 3) {
my ($class, $method, $sub) = @_;
*{"${class}::${method}"} = $sub;
}
else {
my ($method, $sub) = @_;
*$method = $sub;
}
}
}
1;
|