Your comments both in the CB and here helped to find the solution. Following the code which should clearly demonstrate both the the problem and the solution. Thanks for your help!

script_to_test.pl

#!/usr/bin/perl -w use warnings; use strict; use feature qw(switch say); use NaServer; my $session = NaServer->new(); my $val = $session->get_val(); if ($val > 500 ) { say 'Value to high: ' . $val; exit 2; } else { say "Value OK ($val)"; exit 0; }

NaServer.pm - Dummy Module

package NaServer; # this is just a dummy version of NetApps NaServer !! use strict; use warnings; use Carp; sub new { my $class = shift; my $self = {}; bless \$self, $class; } sub get_val { return rand 1000; } 1;

eval.t (this did not work)

#!/usr/bin/perl -w use warnings; use strict; use feature qw(switch say); use Test::More tests => 3; use Test::MockObject; use Test::Output; my $mock = Test::MockObject->new(); $mock->fake_module ('NaServer', new => sub { return 'NaServer' }, get_val => sub { return 500 }, ); use_ok( 'NaServer' ) or exit; ## includes 'use NaServer;' # Construction of $s just for testing my $s = NaServer->new( 'sim8aXXXXXX', 1, 6 ); isa_ok( $s, 'NaServer'); # ================================== # = Tests of the script start here = # ================================== use File::Slurp; my $script = 'script_to_test.pl'; eval 'package Script; sub test {' . read_file($script) . '}'; stdout_is { Script::test() } qq{Value OK (500)\n}, 'stdout as expect +ed'; __END__ ## This does not work as expected, since the test stdout_is is never r +eached. ## Exits in script_to_test.pl prevent this.

trap.t (this one works)

#!/usr/bin/perl -w use warnings; use strict; use feature qw(switch say); use Test::More tests => 3; use Test::MockObject; my $mock = Test::MockObject->new(); $mock->fake_module ('NaServer', new => sub { return 'NaServer' }, get_val => sub { return 500 }, ); use_ok( 'NaServer' ) or exit; ## includes 'use NaServer;' # Construction of $s just for testing my $s = NaServer->new( 'sim8aXXXXXX', 1, 6 ); isa_ok( $s, 'NaServer'); # ================================== # = Tests of the script start here = # ================================== use Test::Trap; use File::Slurp; my $script = 'script_to_test.pl'; my @r = trap { eval read_file($script) }; is ($trap->stdout, "Value OK (500)\n", 'stdout as expected'); __END__ # This works fine, traping after having mocked the NaServer was the tr +ick. # Thanks to the monks!

In reply to Re^2: Trapping exit AND using mocked objects? by LANTI
in thread Trapping exit AND using mocked objects? by LANTI

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.