我在另一个模块中嘲笑子程序比在运行测试时遇到麻烦.
我在一个名为ParserTests.pl的文件中进行了测试.我试图在模块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;
- }
我试图模拟printOut,以便它总是返回true.我想要做的是这样的:
- #! /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;
但是这个测试失败了.你能告诉我为什么并指出我正确的道路让我在测试parse()时正常工作吗?我读了一堆文档,但这样的事情仍然有点不清楚.
错误是
- 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.
那一行(第241行)在printOut子程序中,虽然这意味着它没有像我想要的那样模仿那个子程序.我究竟做错了什么?
解决方法
Test::MockModule
可能更适合这个;
- my $module = Test::MockModule->new('LogParser');
- $module->mock( printOut => sub { return 1 } );
这将导致LogParser使用您的模拟版本,直到$module超出范围.