On Thu Apr 22 04:46:37 2010, Mutant wrote:
Show quoted text> Unless I'm missing something, T::MM doesn't work with objects, i.e. this
> is not possible:
>
> my $mock = Test::MockModule->new('Foo', no_auto=>1);
> $mock->mock(
> 'create' => sub { $mock },
> );
> $mock->mock('bar', sub { print "bah\n" });
>
> my $foo = Foo->new();
> $foo->bar(); # Dies
>
> This should be made clear in the docs. Would be a nice feature, but not
> sure this is the intention of the module.
This seems like you're trying to use Test::MockModule like Test::MockObject. In your example, $mock is actually a Test::MockModule object, not a Foo object. You'll also want to override the 'new' subroutine, rather than 'create'. Somethign like this seems like what you're looking for:
use Test::MockModule;
use strict;
package Foo;
sub foo { print "foo\n"; }
package main;
my $mock = Test::MockModule->new('Foo', no_auto=>1);
$mock->mock(
new => sub { bless {}, "Foo" },
);
$mock->mock('bar', sub { print "bah\n" });
my $foo = Foo->new();
$foo->foo();
$foo->bar(); # Succeeds
Output:
foo
bah