我对属性使用Moose子类型,并希望测试( test ::More)它们对违反约束的输入的正确处理。目前,Mooses的内部错误处理使我的测试文件在看到无效数据时完全停止。
模块源(stackoverflow.com最小化):
package Doctor;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'Phone_nr_t'
=> as 'Str'
=> where { $_ =~ /^\+?[0-9 ]+$/ }
=> message { 'A Phone_nr must be blabla' };
has 'fax' => (is => 'rw', isa => 'Phone_nr_t');测试来源:
use Test::More tests=>1;
use Doctor;
my $testdoc=Doctor->new(fax=>'0341 2345678');
throws_ok { $testdoc->fax('123,456') }
qr('A Phone_nr must be blabla'),
'fax shall reject bad numbers';发布于 2016-01-01 21:12:13
在use strict上发帖之前请先发StackOverflow。您没有包括use Test::Exception;,所以您没有throws_ok。如果将代码包括在内,则代码几乎可以工作。
这样做是可行的:
throws_ok { $testdoc->fax('123,456') }
Moose::Exception::ValidationFailedForInlineTypeConstraint,
'fax shall reject bad numbers';另外,您对正则表达式模式的定义也是错误的。它试图匹配不存在的引号。
throws_ok { $testdoc->fax('123,456') }
qr(A Phone_nr must be blabla),
'fax shall reject bad numbers';https://stackoverflow.com/questions/34559130
复制相似问题