Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock a sub in another module?

I am having troubles mocking a subroutine in another module than where I am running the tests.

I have my tests in a file called ParserTests.pl. I am trying to test a subroutine (parse) in a module LogParser.pm

sub parse {
    my ($self) = @_;
    my $rr = $self->getRR;
    while(1) {
        my $result = $self->parseCommitSet();
        if ($result eq 2) {
            last;
        }
        my $printStatus = $self->printOut($result);
        if (!$printStatus) {
            say "Problem occurred with writing to output";
            return 0;
        }
        $self->setRR(ReportRecord->new());
    }
    return 1;
}

I am trying to mock printOut so that it always returns true. What I am trying to do is this:

#! /usr/bin/perl
use v5.10.0;
use strict;
use warnings;
use Test::More 'no_plan';
use Test::MockObject;
use LogParser;
{other tests…}
my $mock = Test::MockObject->new();
$mock->set_true('LogParser::printOut');
my $test100FH = getTestFH($test100SetsNoPrev);
$logParser = LogParser->new($test100FH);
is($logParser->parse, 1, "im ok?");
close $test100FH;

But this test is failing. Can you tell me why and point me in the right path to get it working correctly for when I test parse()? I read up on a bunch of documentation but something like this is still a bit unclear.

The error is

Can't use an undefined value as a symbol reference at /Users/achu/Documents/workspace/Perl_Script/LogParser.pm line 241, <$fh> line 8371.
# Looks like your test exited with 25 just after 91.

That line (line 241) is inside the printOut subroutine though which means that it's not mocking that subroutine like I wanted it to. What am I doing wrong?

like image 941
AlbChu Avatar asked May 31 '13 15:05

AlbChu


2 Answers

Test::MockObject does not quite do what you want. It is good for supplying a minimally-implemented stub. But for making an instance of the class under test and selectively overriding its methods, you want Test::MockObject::Extends.

TMOE takes an instance and then lets you change what some of its methods do. In your example, you can use it to write the test thus:

use Test::MockObject::Extends;

my $test100FH = getTestFH($test100SetsNoPrev);
$logParser = Test::MockObject::Extends->new(
    LogParser->new($test100FH);
);
$logParser->set_true('printOut');

is($logParser->parse, 1, "im ok?");

close $test100FH;
like image 26
darch Avatar answered Sep 30 '22 20:09

darch


Test::MockModule is probably better suited to this;

my $module = Test::MockModule->new('LogParser');
$module->mock( printOut => sub { return 1 } );

This will cause LogParser to use your mocked version until $module goes out of scope.

like image 172
Steve Sanbeg Avatar answered Sep 30 '22 20:09

Steve Sanbeg