属性に 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';