I prefer to simply encapsulate environmental dependency and redefine it for testing purposes, a test pattern called Test Stub . Test Stub also covers other indirect inputs, such as system time and files. All of them should be interpreted as different forms of the same problem, so I believe that CPAN solutions for this problem are less than great.
As applied to the random number field, we have something like:
use strict; use warnings; package Foo; sub new { my ($class) = @_; return bless {} => $class; } sub get_random_number { return rand(); } package main; use Test::MockObject::Extends; use Test::More tests => 1; my $foo = Test::MockObject::Extends->new( Foo->new() ); $foo->set_series(get_random_number => 0.5, 0.001, 0.999); is( $foo->get_random_number, 0.5 );
This leads to the fact that the system remains unchanged, with the exception of refactoring, which it should have in any case, but provides control points for entering predicted data into the test. get_random_number will not be covered by tests, so it is very important that it be written in such a way as to be accurate when checking; one call to a system resource depends on everything that should be there.
In the case of your specific problem, you need to determine the dependency on rand out from pick , and then override the extracted method in the checked version of List::Gen Test::MockObject::Extends is pretty much perfect for this need.
darch source share