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

Hi, Recently, while writing unit test file for a perl module ( .t file ) I came across a situation where I need to mock a system() call being used in the perl module ( for which this test file is being written ).This system() call is running some c++ code which makes a connectivity with the database. My requirement is that the unit test file should be free from any kind of connectivity from external system ( i.e. DB connectivity here ) and I want to get the return value of this system command as 0 without actually running the command. Is there any way I can mock system() call using any of existing Test::.. perl modules ?
  • Comment on Mocking system() call in Unit Test File for a Perl Module

Replies are listed 'Best First'.
Re: Mocking system() call in Unit Test File for a Perl Module
by jeffa (Bishop) on Nov 21, 2008 at 21:38 UTC

    You can hide the built-in system and use a user defined one like so:

    use subs 'system'; *main::system = sub { 0 };

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)