小编Sve*_*ess的帖子

你如何测试与间接对象语法一起使用的exec?

测试使用间接对象语法调用exec的Perl脚本的最佳方法是什么?有没有办法模拟exec不会导致语法错误?我是否必须将exec移动到仅调用exec并模拟该函数的包装函数?或者有另一种方法来测试脚本吗?

第5章Perl的测试-开发人员的笔记本电脑有一个例子重写内置插件,他们覆盖system测试的模块.我想做同样的测试exec,但是在间接对象语法中.

exec.pl (使用间接对象语法)

package Local::Exec;

use strict;
use warnings;

main() if !caller;

sub main {
    my $shell = '/usr/bin/ksh93';
    my $shell0 = 'ksh93';

    # launch a login shell
    exec {$shell} "-$shell0";
    return;
}

1;
Run Code Online (Sandbox Code Playgroud)

exec.t

#!perl

use strict;
use warnings;

use Test::More;

require_ok('./exec.pl');

package Local::Exec;
use subs 'exec';
package main;

my @exec_args;

*Local::Exec::exec = sub {
    @exec_args = @_;
    return 0;
};

is(Local::Exec::main(), undef, 'main() returns undef');
is_deeply(\@exec_args, [ '/usr/bin/ksh93' …
Run Code Online (Sandbox Code Playgroud)

perl unit-testing

3
推荐指数
1
解决办法
99
查看次数

标签 统计

perl ×1

unit-testing ×1