I'm writing new unit tests for an existing module that uses Log::Log4perl
like:
use Log::Log4perl qw(:easy);
The module calls INFO( "important message" );
. I'd like to mock this to verify from my test code that INFO
is called in certain circumstances.
When I run the test module it doesn't capture the calls to INFO
from the module. What's the right way to mock these calls to INFO
?
Here's a complete example:
Mut.pm
#!/usr/bin/perl -w
# Mut : Module under test
use strict;
use warnings;
package Mut;
use Log::Log4perl qw(:easy);
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
INFO( "Mut::new" );
return $self;
}
1;
Mut.t
#!/usr/bin/perl -w
use strict;
use warnings;
package Mut_Test;
use Test::More tests => 1;
use Test::MockModule;
use Test::MockObject;
my @mock_info_output = ();
my $log4perl = Test::MockModule->new('Log::Log4perl');
$log4perl->mock(
'INFO' => sub {
print STDERR $_[0];
push @mock_info_output, @_;
return;
}
);
BEGIN {
use_ok('Mut');
}
{
my $mut = Mut->new;
## Do something here to verify INFO...
}