Subject: | fake_module() doesn't handle method setup correctly |
Hello chromatic,
while trying to use Test::MockObject-0.09 I found that methods beeing mocked via fake_module() aren't found. Instead I get the error message
Un-mocked method 'method1()' called at package1.pm line xx
To reproduce, please use the following code:
Show quoted text
------ start testscript ------
#!/usr/local/perl/bin/perl -w
use strict;
use Test::MockObject;
use Test::More qw(no_plan);
my $mock = Test::MockObject->new();
$mock->fake_module( 'package1',
'method1' => sub { print "Faking method1() via fake_module()\n" }
);
$mock->fake_new( 'package1' );
# workaround:
#$mock->mock('method1', sub { print "Faking method3() via mock()\n" });
diag( "setting up the mockery" );
use_ok( 'package1' );
my $p1 = package1->new();
is_deeply( $p1, $mock, "new() returns the mockery" );
$p1->method1();
diag( "returned from method1()" );
------ end testscript ------
along with package1 beeing:
------ begin package ------
package package1;
sub new {
my $class = shift;
my $self = {};
bless( $self, $class );
return $self;
}
sub method1 {
my $self = shift;
print "This is the original method1()\n";
}
1;
------ end package ------
The environment is:
> uname -a
HP-UX myhost B.11.00 A 9000/778 2005869654 two-user license
> perl -v
This is perl, version 5.005_03 built for PA-RISC1.1
Looking at the source, I found that the error message is carp()ed only in the AUTOLOAD method. And it's carp()ed only, when the member {_sub}{method1} does not exist. So I added
$class->{_subs}{$sub} = $subs{ $sub };
as the last statement in the foreach loop in fake_module (sorry for not providing a patch -- I don't know how to produce one...). This appeared to solve the problem. The drawback is, that fake_module() becomes an object-method.
Since I don't fully understand the AUTOLOAD mechanism, I'm not sure, whether this patch breaks something else. Moreover, fake_new() just works fine without the patch and it uses fake_module internally.
I would greatly appreciate any attention on this issue, since MockObject would be a big help for my test environment.
Best Regards
Heiko