Hi, I am writing a script for checking a filer. In order to test the script, I need predictable results and therefore use Test::MockObject to mock the answers from the server inside of the test-script. To have this mocked module being used by the tested script, I eval the tested-script, after reading it in by use of File::Slurp.

Bellow some code, the tested script is wrapped in a sub for simplicity.

#!/usr/bin/perl -w use warnings; use strict; use feature qw(switch say); use Test::More 'no_plan'; use Test::Trap; use Test::MockObject; my @r = trap { some_code() }; my $mock = Test::MockObject->new(); $mock->fake_module ('NaServer', new => sub { return 'NaServer' }, set_style => sub ($) {return 'ok'}, set_admin_user => sub ($$) {return 'ok'}, set_timeout => sub ($$) {return 'ok'}, ); use_ok( 'NaServer' ) or exit; my $ob = NaServer->new(); isa_ok ($ob, 'NaServer'); sub some_code { say 'hello world'; #use_ok( 'NaServer' ) or exit; ## commented out by intend!! my $o = NaServer->new(); exit 111; } is ( $trap->exit, 111, 'Exit ist 111');

Any idea how I can trap the exit 111 and still have the code executed in the lexical context of the test-script (or get the mocked NaServer instead of the one, used by the tested-script)?

Just to make things clear: The real script I want to test, has the use NaServer line not commented out, but since the NaServer Module is already loaded in the test-situation its not used again.

Cheers, Ingo


In reply to 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.