http://www.perlmonks.org?node_id=226368

Writing this node reminded me of a testing technique I occasionally find useful.

Consider the following code:

package DeepThought; sub new { bless {}, shift }; sub foo { my $self = shift; $self->bar(42); }; sub bar { my ($self, $n) = @_; print "$self says the answer is $n\n"; };

I want to test that foo calls bar. I could check the text output by bar, but that ties my test to the what bar outputs. Since this can change independently I want to avoid this.

Solution: use local to redefine bar for the scope of the test. For example:

use Test::More tests => 2; isa_ok(my $o = DeepThought->new, 'DeepThought'); { my $ok; no warnings; local *DeepThought::bar = sub { $ok = 1 if $_[1] == 42 }; use warnings; $o->foo(); ok($ok, 'foo called bar'); };

This technique allows you to mock only part of the class you are testing (hence "Micro Mocking" :-). Occasionally very useful.

You can, of course, use the same method for other classes too. For example, if you had:

package Foo; use CGI; sub new { my $class = shift; bless {cgi => CGI->new}, $class; };

You could check that Foo->new called CGI->new like this:

use Test::More tests => 1; { my $called = 0; no warnings; local *CGI::new = sub { $called = 1 }; use warnings; my $o = Foo->new; ok($called, 'Foo->new called CGI->new'); };

But, by this point, Test::MockObject is probably a more sensible choice :-)