Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-MockModule CPAN distribution.

Report information
The Basics
Id: 77439
Status: resolved
Priority: 0/
Queue: Test-MockModule

People
Owner: GFRANKS [...] cpan.org
Requestors: FBRIERE [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.06



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();
On Fri May 25 22:08:34 2012, FBRIERE wrote: Show quoted text
> 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.)
I believe I have a fix for this on my fork of Test::MockModule: https://github.com/geofffranks/test-mockmodule/ When unmocking, the method will removed, resulting in dispatch to the parent module. When calling original() for a subroutine that had been inherited, Test::MockModule will return the parent module's current corresponding subroutine, regardless of what changes may happen to the parent module. This probably is less confusing, if you look at t/inheritance.t
A fix for this was released today in 0.06 of Test::MockModule