I use Moose subtypes for attributes and want to test (Test::More) their correct handling of constraint-violating input. Currently Mooses's internal error handling makes my testfile stop completely when it sees the invalid data.
Module source (minimized for 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');
Test source:
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';
Please use strict
before you post on StackOverflow. You have not included use Test::Exception;
so you don't have throws_ok
. If you include that your code almost works.
This works:
throws_ok { $testdoc->fax('123,456') }
Moose::Exception::ValidationFailedForInlineTypeConstraint,
'fax shall reject bad numbers';
Also your definition of the regular expression pattern is wrong. It tries to match quotes which don't exist.
throws_ok { $testdoc->fax('123,456') }
qr(A Phone_nr must be blabla),
'fax shall reject bad numbers';