http://www.perlmonks.org?node_id=838888

LANTI has asked for the wisdom of the Perl Monks concerning the following question:

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