vendredi 1 janvier 2016

How do I test Moose subtype error messages?

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';

Aucun commentaire:

Enregistrer un commentaire