[Israel.pm] Testing problem

Jason Elbaum Jason.Elbaum at motorola.com
Mon Jan 5 05:45:32 PST 2004

Thomas Maier wrote:

> sub method 
> {
> 	my ($self, at param) = @_;
> 	$pid = open $fh,"-|","command @param";
> 	$x = do something with $fh;
> 	return $x;
> }
> I would like to test this method but without
> actually running the 'command'. 

I haven't thought this through to the end, but here are some preliminary 

First, you need to separate the running of the command from the method. 
One way to do this:

sub method
	my ($self, $commandSubRef, @param) = @_;

         # Execute command, creating a pid and a file handle
	($pid, $fh) = $commandSubRef->(@param);

	$x = do something with $fh;
	return $x;

Then your production code can call the method like this:

sub command
  	my @param = @_;
	$pid = open $fh,"-|","command @param";
	return ($pid, $fh);

$self->method(command, @param);

Your test code can provide an alternative implementation of command 
which, for example, reads a predefined file. For example:

sub commandTester
  	my @param = @_;

	$pid = open $fh,"-|","cat testfile";
	return ($pid, $fh);

Is this helpful?

Jason Elbaum

More information about the Perl mailing list