ErrorLibrary.pm:
package Artopi::Builder::ErrorLibrary;
use strict;
use warnings;
use constant {
# wiki link included as a variable in this example
CABLING_ERROR => {
errorCode => 561,
message => "cabling is not correct\n\n ",
t => { template => 'disabled'},
page =>'http://w.error-sol.com/index/Builder/ErrorCodes/_CABLING_ERROR',
},
};
这是我尝试测试的error_post方法。我正在尝试进行单元测试,以便预期输出正确。我目前的测试不能正确编译,我无法看到我的代码出现了什么问题。这可能是我应该能够看到/知道的东西。
ErrorPost.pm:
package Artopi::Builder::ErrorPost;
use strict;
use warnings;
use Artopi::Builder::ErrorLibrary;
# takes error name as a param and prints out the message contained in the error hash.
sub error_post {
my($error) = @_;
print ($error->{ message });
}
1;
以下是我提出的当前测试。但它给我一个错误说:'不能使用字符串Artopi :: Builder :: ErrorPost作为HASH参考,而#34; strict refs"在/ErrorPost.pm第12行使用
error_post.t :
my $error = Artopi::Builder::ErrorLibrary->CABLING_ERROR;
# expected values
my $exp_output = ($error->{message});
my $exp_input = Artopi::Builder::ErrorLibrary->CABLING_ERROR;
# input value as a parameter of error_post method
my $error_in = Artropi::Builder::ErrorPost->error_post($exp_input);
# checking that exp_input matches the expected output after the output has
# been passed through the error_post method
is($error_in, qr/$exp_output/, 'This is the correct output');
有什么建议吗? :)
答案 0 :(得分:1)
您可以使用Test::Output来测试打印的内容。
.container{
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
}
.flex-item{
width: 50%;
@media (max-width: 600px){
width: 100%;
}
}
在这种情况下,您的sub的返回值根本不会被测试。如果你想要两者,你必须将它保存在你的匿名子中并稍后进行测试。
use Test::More;
use Test::Output;
sub error_post {
my ($error) = @_;
print( $error->{message} );
}
stdout_is { error_post( { message => 'foo' } ) } "foo",
'the error message is correct';
# equivalent without the bare block/map-ish syntax
stdout_is(
sub { error_post( { message => 'foo' } ) },
"foo",
'the error message is correct'
);
# and using like
stdout_like { error_post($error) } qr/foo/,
'the error message looks correct';
done_testing;
测试::输出使用引擎盖下的Capture::Tiny,如果您只需要在无法控制的代码中输出消失,那就太棒了。