Subject: | Test::MockObject::Externds does not clear its calls |
The objects created with Test::MockObject::Extends do not call
Test::MockObject::DESTROY and, as such, their calls are not cleared.
This will be a problem if a new Test::MockObject is allocated the same
address later.
The attached program demonstrates the behaviour: it Extends an object
and lets it go out of scope. When another object is created, it will
likely be allocated the same address in memory, and report call_pos and
friends from the first one.
This is not a problem with normal Test::MockObject, which destroys the
calls in DESTROY.
The expected output from the program is:
Created object: Test::MockObject=HASH(0x9be88c8)
Created object: Test::MockObject=HASH(0x9be88c8)
Created object: T::MO::E::a=HASH(0x9be88c8)
Created object: T::MO::E::b=HASH(0x9be88c8)
The address pairs should be the same, but no extra lines should be
printed.
However, the actual output is:
Created object: Test::MockObject=HASH(0x9be88c8)
Created object: Test::MockObject=HASH(0x9be88c8)
Created object: T::MO::E::a=HASH(0x9be88c8)
Created object: T::MO::E::b=HASH(0x9be88c8)
test
The last line indicates the newly-created object inherited call records
from the previous one.
Subject: | mockobject.pm |
#!/usr/bin/env perl
use warnings;
use strict;
use Test::MockObject;
use Test::MockObject::Extends;
sub mocked {
my $obj = Test::MockObject->new();
print "Created object: $obj\n";
print $obj->call_pos(1) . "\n" if $obj->call_pos(1);
$obj->set_true('test');
$obj->test();
}
mocked();
mocked();
sub extended {
my $obj = bless {}, 'Ggg';
$obj = Test::MockObject::Extends->new($obj);
print "Created object: $obj\n";
print $obj->call_pos(1) . "\n" if $obj->call_pos(1);
$obj->set_true('test');
$obj->test();
}
extended();
extended();