On Fri, 2005-06-10 at 20:40 -0400, via RT wrote:
Show quoted text> This message about Test-MockObject was sent to you by ADAMK <ADAMK@cpan.org> via rt.cpan.org
>
> Full context and any attached attachments can be found at:
> <URL:
https://rt.cpan.org/Ticket/Display.html?id=13200 >
>
> I get myself in a corner... a really nasty one. Untestable.
>
> Without something like Test::MockObject, yay!
>
> I'll try it out for the first time. It will save me.
>
> my $Object = Parent->new;
> my $Mock = Test::MockObject::Extends->new( $Object );
> $Mock->mock('mockthis', sub { return 'foo' } );
> my $rv = $Mock->foo;
> is( $rv, "foo", "Mocking worked" );
> is( $Parent::somethingnasty, 'Method didn't trigger bad method' );
>
> package Parent;
>
> sub new {
> bless { }, $_[0];
> }
>
> sub AUTOLOAD {
warn "<$_[0]>\n";
Show quoted text> return $_[0]->mockthis();
> }
>
> our $somethingnasty = '';
> sub mockthis {
> $somethingnasty = 1;
> }
>
> 1;
>
>
> And so with my very first method call via mock, I appear to have hit either a bug or a limitation?
It's a limitation. The invocant is always a member of the extended
object to all methods of that class. Any method you don't mock on the
T::MO::E object passes through to the wrapped object. In my previous
design attempts, it was always too fragile to do otherwise, working
correctly about half of the time and failing dramatically the rest of
the time.
Think of it as implementing the Decorator pattern.
Show quoted text> You don't check for AUTOLOAD in the things you mock. Is this by intent?
Yes. If you need AUTOLOAD() to work, either predeclare your subs or
override your can() to do the right thing.
-- c