3

I am trying to write a perl unit test. I am able to test happy-case scenario for it. However if there is an error generated in the method it prints the error using Carp:confess "<message>". I am not able to catch this case in my test. I tried using

dies_ok( <method call>, 'Expected Error' );

However the test case still fails. It prints the message passed to Carp::confess and then prints

Looks like your test exited with 111 before it could output anything. Dubious, test returned 111 (wstat 28416, 0x6f00)

Is there a way I can catch this? I even tried throws_ok but not working.

Please assist by guiding how I should catch these errors. Am I using these dies_ok and throws_ok incorrectly ?

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69
alwaysAStudent
  • 2,110
  • 4
  • 24
  • 47
  • 2
    did you wrap the method call in an anonymous sub? – Miller Nov 19 '15 at 00:22
  • No I hadn't... I did that and it worked...Can you please explain why a anonymous sub is required to be encapsulating the call? I am not clear on that... Also can you please post the explanation as answer so I can accept it. Thanks :) – alwaysAStudent Nov 19 '15 at 00:53

1 Answers1

1

You may just check $@ after eval expression.

use strict;
use warnings;

use Test::More;

use Carp qw(confess);

sub err { confess('Bad thing'); }

eval { err };
like($@, qr/^Bad thing/, "confess('Bad thing')");

done_testing();
edem
  • 3,222
  • 3
  • 19
  • 45