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

So I have some code I'm testing which relies on a class with a whole bunch of niggling, annoying little methods getting called hither and yon. For the purposes of my tests, however, I really only care about one accessor at the end of the test and what it returns (since some methods are called in void context). I don't care to go through the trouble of mocking all of the other methods. I'm looking for (and can't find) a test version of Class::BlackHole. I'm thinking something like this:

use Test::Automock; my $mock = Test::Automock->('Some::Difficult::Module'); $mock->add( fetch_my_slippers => sub { 1 } ); # adds this method can_ok $object, 'stuff'; ok $object->stuff, '... and calling it should succeed'; my @expected = ( fribble => [ $mock, $object ], woobie => [ $mock ], ); is_deeply $mock->methods_called, \@expected, "... with Some::Difficult::Methods doing their thing"; $mock->reset;

Test::Automock would simply use autoload to capture all method calls and their arguments. Methods by default would return a true value unless specifically overridden. I'd have to do tricks like overriding isa() and friends, but that seems like it would be a very lightweight method of handling mocked objects.

Is there something which does this? Did I miss anything?

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Automocked objects
by jasonk (Parson) on Apr 17, 2007 at 12:00 UTC

    I do this by subclassing Test::MockObject...

    package Test::AutoMock; use base 'Test::MockObject'; # This is a stripped-down version of some of my own testing # code, so this is untested, but should work... sub dispatch_mocked_method { my $self = $_[0]; my $sub = splice( @_, 1, 1 ); my $subs = Test::MockObject::_subs( $self ); $self->log_call( $sub, @_ ); if ( exists $subs->{ $sub } ) { goto &{ $subs->{ $sub } }; } else { return 1; } }

    dispatch_mocked_method is called by Test::MockObject::AUTOLOAD, and carps when you call a method that hasn't been mocked, so if you just replace the carp with a return 1; you get the 'return true unless overridden' behaviour. I also moved the log call outside the if, so that the call log can include both mocked and default method calls.


    We're not surrounded, we're in a target-rich environment!
Re: Automocked objects (chains)
by tye (Sage) on Apr 17, 2007 at 16:01 UTC

    If you make a module out of this, it might be best to use the object itself as the default "true" return value. That way chained method calls will work.

    - tye