【发布时间】:2016-07-24 04:50:14
【问题描述】:
我正在尝试测试以下方法的输出:
package ASC::Builder::Error;
sub new {
my ($package, $first_param) = (shift, shift);
if (ref $first_param eq 'HASH') {
my %params = @_;
return bless { message => $first_param->{message}, %params}, $package;
}
else {
my %params = @_;
return bless {message => $first_param, %params}, $package;
}
}
此方法应该接受错误哈希或错误字符串。如果它接受一个散列,它应该从错误散列中输出消息键的值。
这是位于 ErrorLibrary.pm 中的错误哈希:
use constant {
CABLING_ERROR => {
code => 561,
message => "cabling is not correct at T1",
tt => { template => 'disabled'},
fatal => 1,
link =>'http://www.e-solution.com/CABLING_ERROR',
},
};
这是消息方法以及位于 Error.pm 中的散列的其他键
package ASC::Builder::Error;
sub message {
return $_[0]->{message};
}
sub tt {
return {$_[0]->{tt} };
}
sub code {
return {$_[0]->{code} };
}
这是我当前位于 error.t 中的单元测试
#input value will either be a String or and Error Message Hash
# error hash
my $error_hash = CABLING_ERROR;
# error string
my $error_string = "cabling is not correct at T1.";
# error hash is passed into new and an error object is outputted
my $error_in = ASC::Builder::Error->new($error_hash);
# checks to see if the output object from new is an Error object
isa_ok($error_in, 'ASC::Builder::Error');
# checking that object can call the message() method
can_ok( $error_in, 'message');
# checks to see if the output message matches the message contained in the error hash(correct)
is($error_in->message(),( $error_string || $error_hash->{message} ), 'Returns correct error message');
最后是我的测试结果:
# Failed test 'Returns correct error message'
# at t/67_error_post.t line 104.
# got: 'HASH(0x38b393d490)'
# expected: 'cabling is not correct at T1.'
#
# '
# Looks like you failed 1 test of 3.
t/67_error_post.t .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests
【问题讨论】:
-
您可能希望正确缩进您的代码。有点难读。
标签: perl unit-testing hash return-type hash-of-hashes