Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Planning for deaths in perl tests [duplicate]

Is there a way to write tests for Perl calls that you expect to die? I'd like to verify that certain calls will die with poorly formatted inputs.

sub routine_a {
   my $arg = shift;
   die if $arg eq 'FOO';
   print "routine_a: $arg\n";
}
sub routine_b {
   my $arg = shift;
   die if $arg eq 'BAR';
   print "routine_b: $arg\n";
}

sub test_all {
   assert( routine_a("blah") );
   assert( routine_b("blab") );
   assert_death( routine_a("FOO") );
   assert_death( routine_b("BAR") );
}
like image 863
ajwood Avatar asked Jun 04 '12 16:06

ajwood


1 Answers

See Test::Exception:

use Test::Exception;
dies_ok { $foo->method } 'expecting to die';
like image 167
zoul Avatar answered Oct 14 '22 08:10

zoul