Subject: | unmock() on an inherited method does not behave properly |
When unmocking a method that was not originally defined in the mocked
module, but rather inherited from a parent class, unmock() will install
a copy of the inherited method in the child class. This will subtly
break if the parent method ever changes, such as illustrated in the
attached example.
I think that mock() should not bother with can() if &{$sub_name} does
not exist, and merely store undef. (OTOH, it may be argued that
original() should returned the inherited method. Hmm.)
Subject: | mock.pl |
#!/usr/bin/perl
use 5.10.0;
use Test::MockModule;
@Bar::ISA = 'Foo';
@Baz::ISA = 'Bar';
sub Foo::motto { 'Foo!' };
say Foo->motto(), Bar->motto(), Baz->motto();
{
my $mock_bar = new Test::MockModule('Bar', no_auto => 1);
$mock_bar->mock('motto', sub { 'Bar!' });
my $mock_baz = new Test::MockModule('Baz', no_auto => 1);
$mock_baz->mock('motto', sub { 'Baz!' });
say Foo->motto(), Bar->motto(), Baz->motto();
}
say Foo->motto(), Bar->motto(), Baz->motto();