"Perl for Perl Newbies" - Part 5 - Good Programming PracticesAutomated TestingMocking

2.4. Mocking

When testing certain parts of the application, it is sometimes desirable to mimic the functionality of different parts, so the testing will be isolated. For example, if we're testing a server-side script (such as a CGI script), we may wish to provide a server-emulating object that's completely under our control and that inputs the script with our own parameters. This is called mocking(see the Wikipedia article about Mock objects), and there are several mechanisms for doing so for Perl facilities:

With regard to mocking modules, one may opt to simulate loading a module using the Perl %INCvariable (see perlvar) by doing something like:

use strict;
use warnings;


# .
# .
# .


} =


After doing this, the tested code can do use CGI;and still think it loaded the original module, while actually it is using our own mocked version.

Written by Shlomi Fish