简体   繁体   English

如何在另一个模块中模拟子?

[英]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. 我在一个名为ParserTests.pl的文件中进行了测试。 I am trying to test a subroutine (parse) in a module LogParser.pm 我试图在模块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. 我试图模拟printOut,以便它总是返回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()? 你能告诉我为什么并指出我正确的道路让我在测试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. 那一行(第241行)在printOut子程序中,虽然这意味着它没有像我想要的那样模仿那个子程序。 What am I doing wrong? 我究竟做错了什么?

Test::MockModule is probably better suited to this; Test::MockModule可能更适合这个;

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. 这将导致LogParser使用您的$module版本,直到$module超出范围。

Test::MockObject does not quite do what you want. Test::MockObject并不能完全按照你的意愿Test::MockObject 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 . 但是为了使测试中的类的实例并选择性地重写其方法,您需要Test::MockObject::Extends

TMOE takes an instance and then lets you change what some of its methods do. TMOE接受一个实例,然后让您更改其某些方法的功能。 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;

You didn't provide the error message, but what you've defined is an object called $mock that contains a 'printout' method. 您没有提供错误消息,但您定义的是一个名为$ mock的对象,其中包含'printout'方法。 But you're calling printout() on $logparser. 但是你在$ logparser上调用printout()。

The point of MockObject is to create a very bare object, with a few methods so you can test other pieces of code in a algorithm that relies on an external object. MockObject的要点是使用一些方法创建一个非常简单的对象,这样您就可以在依赖于外部对象的算法中测试其他代码片段。 For example, you could mock a database handle so that calling $dbh->fetchStuff() always returns on static row, so that you can test the code that consumes the row. 例如,您可以模拟数据库句柄,以便调用$ dbh-> fetchStuff()始终在静态行上返回,以便您可以测试使用该行的代码。

So without more context, I can't tell the possibilities for just creating a stub for printOut() so that parse knows about it. 因此,如果没有更多上下文,我无法分辨为printOut()创建存根的可能性,以便解析知道它。

That being said, I also don't understand the desire to have a test for the return value of the stubbed method. 话虽这么说,我也不理解对短线方法的返回值进行测试的愿望。

Please read the documentation for Test::MockObject and try to understand how it works. 请阅读Test :: MockObject的文档,并尝试了解它的工作原理。

You are doing only the first half of what is actually required: You are creating a mock object. 您只是在实际需要的前半部分:您正在创建一个模拟对象。 But this will not magically end up in your LogParser. 但这不会神奇地结束在你的LogParser中。

What Test::MockObject gives you is an object that behaves just like the object you want to mock. Test::MockObject给你的是一个对象,其行为就像你想要模拟的对象一样。 Of course, somebody or something still has to use that object. 当然,有人或某些东西仍然必须使用该对象。 And this will have to be the code you are trying to test. 这将是您要测试的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM