Skip Menu |

This queue is for tickets about the Test-MockObject CPAN distribution.

Report information
The Basics
Id: 1290
Status: resolved
Worked: 10 min
Priority: 0/
Queue: Test-MockObject

People
Owner: chromatic [...] cpan.org
Requestors: Heiko.Rau [...] t-systems.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.09
Fixed in: (no value)



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
From: chromatic <chromatic [...] wgz.org>
To: bug-Test-MockObject [...] rt.cpan.org
Subject: Re: [cpan #1290] fake_module() doesn't handle method setup correctly
Date: Tue, 20 Aug 2002 21:42:30 -0700
On Tuesday 16 July 2002 08:10, you wrote: Show quoted text
> 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
Right, that's an artifact of the current design. It makes a distinction between modules and objects. (The former has a function-oriented interface and the latter has an object-oriented interface.) I'm not completely pleased with the idea of keeping fake_module() in Test::MockObject... it seems different enough it should be in a separate module. Show quoted text
> $mock->fake_new( 'package1' ); > > # workaround: > #$mock->mock('method1', sub { print "Faking method3() via mock()\n" });
This is the recommended way to do it. fake_new() allows you to intercept calls to new() for the mocked package, so you can mock methods on it. Show quoted text
> The drawback is, that fake_module() becomes an object-method.
That's my main objection to doing this with Test::MockObject. The workaround you discovered is how I handle this. At some point, I or someone else will come up with a sane interface for Test::MockModule, and that will be a better way to do things. Thanks for the report, -- c