Mutant has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've got a class that calls methods like $self->method, which I'd like to write tests for. Because the internal method call might have state data I don't want to setup, I'd like to mock this method. For this, I thought I could use Test::MockObject::Extends.

Unfortunately, it looks like T::MO::E doesn't mock these methods. As an example, look at this class:
use strict; use warnings; package Foo; sub new { my $class = shift; return bless {}, $class; } sub bar { my $self = shift; $self->baz; return 1; } sub baz { die; } 1;
(This has to be in a separate file if you want to test this, because T::MO::E tries to 'use' the module when you call new()).

The test script below should be ok, but instead it dies (when bar() internally calls baz(), which I've attempted to mock):
use strict; use warnings; use Foo; use Test::MockObject::Extends; my $foo = Foo->new(); my $mock_foo = Test::MockObject::Extends->new( $foo ); $mock_foo->mock('baz',sub { 1 }); $mock_foo->bar;
Am I totally missing the point of Test::MockObject::Extends? Is my code untestable and in need of refactoring (probably, but not necessarily practical)? Any other suggestions?

Thanks.

Replies are listed 'Best First'.
Re: Test::MockObject::Extends question
by chromatic (Archbishop) on Jul 15, 2005 at 17:15 UTC

    Are you using version 1.00? I can't reproduce the error with that version and think I fixed the bug in the latest release.

      My apologies. I was indeed running an old version.

      Thanks.