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: 84186
Status: rejected
Priority: 0/
Queue: Test-MockModule

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

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



Subject: unmock_all umocks mocked methods for all objects that mock the class
Date: Mon, 25 Mar 2013 00:07:00 -0400
To: bug-Test-MockModule [...] rt.cpan.org
From: "David E. Wheeler" <dwheeler [...] cpan.org>
Example code: use Test::MockModule; my $mock1 = Test::MockModule->new('FileHandle'); $mock1->mock(new => 'foo'); my $mock2 = Test::MockModule->new('FileHandle'); $mock2->mock(new => 'bar'); say FileHandle->new; $mock2->unmock_all; say FileHandle->new; Output: bar FileHandle=GLOB(0x7f8152865e28) I would expect the output to be: bar foo The bug is that, even though I call unmock_all on an object, expecting it to just remove the mocks created for *that* object, it ends up removing the mocks for *all* objects that have mocked the same class.
On Mon Mar 25 00:07:09 2013, DWHEELER wrote: Show quoted text
> Example code: > > use Test::MockModule; > > my $mock1 = Test::MockModule->new('FileHandle'); > $mock1->mock(new => 'foo'); > > my $mock2 = Test::MockModule->new('FileHandle'); > $mock2->mock(new => 'bar'); > > say FileHandle->new; > $mock2->unmock_all; > say FileHandle->new; > > Output: > > bar > FileHandle=GLOB(0x7f8152865e28) > > I would expect the output to be: > > bar > foo > > The bug is that, even though I call unmock_all on an object, expecting > it to just remove the mocks created for *that* object, it ends up > removing the mocks for *all* objects that have mocked the same class.
The problem here is that the mocking happens at the module namespace, and object is keeping track of its own data. In order to do what you're requesting, each Test::MockModule object would need to know about each other, and be notified when the other one is destroyed, so that it can re-mock accordingly. As an alternative, I'd suggest you use one mock object, and do something like this: use Test::MockModule; my $mocked_foo = sub { return "foo" } my $mock = Test::MockModule->new('FileHandle'); $mock->mock(new => $mocked_foo); say FileHandle->new; $mock->mock(new => 'bar'); say FileHandle->new; $mock->mock(new => $mocked_foo);