I am trying to construct a convenient mocking class using moops:
#!/usr/bin/env perl
use Modern::Perl '2014';
use Moops;
use Test::More;
class aClass {
method m {}
method l {}
};
class NotWorkingMockAClass
extends aClass {
has methodCallLog => (
is => 'rw',
default => sub { [] },
isa => ArrayRef
);
around m, l {
push $self->methodCallLog, (caller(0))[3] =~ m/::(\w+)$/;
$next->($self, @_ );
}
};
my $mac = NotWorkingMockAClass->new();
$mac->m();
$mac->l();
$mac->m();
is( ($mac->methodCallLog)->[0], 'm', 'mcl[0] == m' );
is( ($mac->methodCallLog)->[1], 'l', 'mcl[1] == l' );
is( ($mac->methodCallLog)->[2], 'm', 'mcl[2] == m' );
This yields:
$ perl mocking.pl
ok 1 - mcl[0] == m
not ok 2 - mcl[1] == l
# Failed test 'mcl[1] == l'
# at mocking.pl line 33.
# got: 'm'
# expected: 'l'
ok 3 - mcl[2] == m
So, the problem seems to be, that caller()
always returns m
, when I use the around m,l ..
shortcut.
Defining the class like so:
class WorkingMockAClass
extends aClass {
has methodCallLog => (
is => 'rw',
default => sub { [] },
isa => ArrayRef
);
method _logAndDispatch( CodeRef $next, ArrayRef $args ){
push $self->methodCallLog, (caller(1))[3] =~ m/::(\w)$/;
$next->($self, @$args );
}
around m {
$self->_logAndDispatch( $next, \@_ );
}
around l {
$self->_logAndDispatch( $next, \@_ );
}
};
works, but is a bit more verbose and cumbersome to write.
Is there a better option to achieve something like this with Moops?
Personally Moops or otherwise, I wouldn't trust
caller
in any method that is potentially going to have modifiers applied to it. Nor would I trust it in those modifiers. You're relying too much on the internals of how method modifiers work. (Which will differ between Moo/Moose/Mouse.)Have you tried something like this?
(Or use Sub::Util instead of Sub::Identify.)