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

I'm using a mock object to test a module. The module requires, as an input, a Foo object, and the module has a gaurd clause to assert that the foo argument isa Foo. Now, in testing, I am sending in a mock Foo, but I'd like the mock Foo to pass the isa test:  $mock->isa('Foo').

I can't seem to get this to work.

Note in the small example below, I can mock bar, but I can't mock isa. (And yes, I know my mocked isa is always true as written -- so that $mock->isa('Foo') and $mock->isa('NotAFoo') would both be true. Yes, I'll fix my mock isa, but for now, I can't even get the mock isa called.... I never see the warning.)

THanks for any advice

use Test::MockObject; use Test::More 'no_plan'; use strict; my $x = Test::MockObject->new( {} ); $x->fake_module('Foo'); $x->fake_new('Foo'); $x->mock( 'isa', sub { warn 'I dont get this warning'; 1 } ); $x->mock( 'bar', sub { 2 } ); is( $x->isa('Foo'), 1 ); is( $x->bar('Foo'), 2 );

Replies are listed 'Best First'.
Re: Mocking isa
by chromatic (Archbishop) on Apr 27, 2004 at 17:56 UTC

    Try Test::MockObject::Extends:

    use Test::MockObject; use Test::MockObject::Extends; use Test::More 'no_plan'; use strict; Test::MockObject->fake_module( 'Foo' ); my $x = Test::MockObject::Extends->new( 'Foo' ); $x->mock( 'bar', sub { 2 } ); is( $x->isa('Foo'), 1 ); is( $x->bar('Foo'), 2 );
Re: Mocking isa
by blokhead (Monsignor) on Apr 27, 2004 at 17:53 UTC
    This is mentioned in the TODO section of Test::MockObject. You may want to see if Test::MockObject::Extends will work for your needs instead (it will handle isa properly, although I don't know if you'll be able to override its behavior).

    blokhead