in reply to mocking or trapping system calls

Chapter 5 of Perl Testing, A Developer's Notebook (O'Reilly: Ian Langworth and chromatic), "Testing Untestable Code" has a bit on over-riding the system() built-in with a vanilla Perl subroutine. You build a dispatch table in the subroutine to manage the commands you explicitly want to test, and make sure that the last lines of the table are::
} else { print("Un-handled command -- $the_command\n"); return(-1); }

Note to Ian and chromatic -- after reading your book I have come to the conclusion that there is **nothing** that is un-testable. You just have to look in the right chapter of PTaDN.... </code>

----
I Go Back to Sleep, Now.

OGB

Replies are listed 'Best First'.
Re^2: mocking or trapping system calls
by dpchrist (Initiate) on Mar 20, 2014 at 22:54 UTC

    I'm trying to apply the mocking technique shown on pp. 85-86 of "Perl Testing A Developer's Notebook" using Perl 5.14.2. It works for module subroutines, but not for system():

    2014-03-20 15:53:01 dpchrist@p43200 ~/sandbox/perl $ cat mock-system.pl #! /usr/bin/perl use strict; use warnings; $| = 1; package Foo; sub foo { system @_ } package main; print Foo::foo("echo", "-n", __LINE__ . "\n"), "\n"; { package Foo; use subs "system"; package main; *Foo::system = sub { print "mock system() called: @_"; 1 }; print Foo::foo("echo", "-n", __LINE__ . "\n"), "\n"; } { package Foo; use subs "foo"; package main; *Foo::foo = sub { print "mock foo() called: @_"; 2 }; print Foo::foo("echo", "-n", __LINE__ . "\n"), "\n"; } 2014-03-20 15:53:06 dpchrist@p43200 ~/sandbox/perl $ perl mock-system.pl 12 0 19 0 Subroutine Foo::foo redefined at mock-system.pl line 26. mock foo() called: echo -n 27 2

    Does anybody know why the technique doesn't work for system()?