Permalink
Browse files

Fix EasyMock::MockControl::Class.

  • Loading branch information...
1 parent 219cd16 commit 30b9bd0df484ed252ea2094c9f9a9121ee6cbf66 @NetPenguin committed Aug 18, 2013
View
@@ -208,7 +208,7 @@ sub __delegate {
my ($method, $mock, @args) = @_;
my $control = __control_of($mock)
or confess('Speocified mock is not under management');
- return $control->$method(@args);
+ return $control->$method($mock, @args);
}
sub __control_of {
@@ -25,6 +25,16 @@ sub new {
}, $class;
}
+=head1 PROPERTIES
+
+=head2 method - An expected method name.
+
+=cut
+sub method {
+ my ($self) = @_;
+ return $self->{_method};
+}
+
=head1 METHODS
=head2 push_result($code)
@@ -139,7 +139,7 @@ Record the expectation of the mock method invocation.
=cut
sub expect {
- my ($self, $expectation) = @_;
+ my ($self, $mock, $expectation) = @_;
push @{$self->{_expectations}}, $expectation;
return Test::EasyMock::ExpectationSetters->new($expectation);
}
@@ -2,6 +2,11 @@ package Test::EasyMock::MockControl::Class;
use strict;
use warnings;
+=head1 NAME
+
+Test::EasyMock::MockControl::Class - Control behavior of the class method mocking.
+
+=cut
use parent qw(Test::EasyMock::MockControl);
use Carp qw(confess);
use Scalar::Util qw(weaken);
@@ -21,13 +26,13 @@ sub replay {
my $self = shift;
$self->SUPER::replay(@_);
+ # prevent circular reference
my ($mock) = @_;
+ weaken($mock);
+
my $mock_module = Test::MockModule->new($self->{_module});
foreach my $expectation (@{$self->{_expectations}}) {
my $method = $expectation->method;
- # prevent circular reference
- weaken($mock);
-
$mock_module->mock($method => sub {
my $class = shift;
return $mock->$method(@_);
View
@@ -10,9 +10,6 @@ BEGIN {
use t::Foo;
# ----
-# Helper.
-
-# ----
# Tests.
subtest 'mock class method.' => sub {
my $mock = create_class_mock('t::Foo');
@@ -54,21 +51,9 @@ subtest 'mock class method.' => sub {
reset($mock);
subtest 'after gc' => sub {
- {
- expect($mock->foo(1))->and_stub_scalar_return('a');
- replay($mock);
- my $control = $mock->{_control};
- my $x = $mock;
- use Scalar::Util qw(weaken isweak);
- weaken($control);
- weaken($x);
- warn "BEFORE: CONTROL: $control";
- $mock = undef;
- undef($mock);
- warn "AFTER: CONTROL: $control";
- warn "AFTER: MOCK: $x\n", isweak($x);
- use Data::Dumper; warn Dumper($x);
- }
+ expect($mock->foo(1))->and_stub_scalar_return('a');
+ replay($mock);
+ $mock = undef;
is(t::Foo->foo(1), 'original-foo', 'result');
};
};

0 comments on commit 30b9bd0

Please sign in to comment.