UPDATE:I discovered Test::Fatal and used it like so:

like(exception{ $checker->_get_event_data('qwerty.pl') }, qr/Failed to + retrieve data from file/, 'file does not exist');

logf subroutine required following adjustment:

$logger->fatal("(Line: $line) " . $error ); print "\n"; open (STDOUT, '>/dev/null'); #squelch duplicate error output die $error;

END UPDATE

I've got a simple subroutine utility that I've been using with all my scripts that prints out the details of a script failure using the Log::Log4Perlmodule:

sub logf { my $error = shift; my $line = shift; if (!$line) { $line = [caller(0)]->[2]; } $logger->fatal("(Line: $line) " . $error); die "\n"; }

I want to test the following bit of code:

sub _get_event_data { my $self = shift; my $file = shift; my $data; eval { $data = retrieve($file); }; if ($@) { logf('Failed to retrieve data from file'); ### Line that calls fai +lure subroutine above } my $existing_events = $data->{existing_events}; return $existing_events; }

I want to test to see if the module returns the correct error messages. Here is the test:

throws_ok{ $checker->_get_event_data('qwerty.pl') } qr/Failed to retri +eve data from file/, 'file does not exist';

The test never passes, however because it has no idea what the $logger object printed. I tried changing the logf() function to:

die ($logger->fatal("(Line: $line) " .  $error));

This did not get the test to pass, however. The following worked but the test script dies and the remaining tests don't run:

$logger->fatal("(Line: $line) " . $error); die "$error\n";

So am I going to have to abandon my user of the Log4Perl module to be able to test the errors my script generates? Is there something else I might try?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks


In reply to SOLVED: Testing output of logger output when dying by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.