PopeFelix has asked for the wisdom of the Perl Monks concerning the following question:

I have a method, let's call it "foo" defined in a Moo Role.

package 'MyRole'; use Moo::Role; sub foo { return 'blah'; }

In a consuming class, I have some behavior implemented in an "around" modifier:

package MyClass; use Moo; with 'MyRole'; around foo => sub { my ($self, $orig) = @_; if ($self->$orig eq 'baz') { return 'bak'; } return $self->$orig; }

I would like to test this behavior, but I can't figure out how to mock MyRole::foo. If I mock MyClass::foo, the around() modifier isn't called. How do I test this around() behavior?

Here's what I'm trying:

use MyClass; use Test::Most; use Test::MockModule; my $mock = Test::MockModule->new('MyRole'); $mock->mock('foo' => sub { return 'baz' }); my $obj = MyClass->new; # Does not work is $obj->foo, 'bak', 'Foo is what it oughtta be';

Replies are listed 'Best First'.
Re: Mocking a method defined in a Moo Role
by choroba (Cardinal) on Jun 23, 2016 at 19:09 UTC
    What exactly do you want to test? You don't want to test that around works, it's been already tested in Class::Method::Modifiers. You want to test your class and your role separately.

    Here's my example (because yours had several problems, e.g. switched $self and $orig in 'around'):

    MyRole.pm:

    package MyRole; use Moo::Role; sub foo { $_[1] ? 'true' : 'false'; } __PACKAGE__

    MyClass.pm

    package MyClass; use warnings; use strict; use Moo; with 'MyRole'; around foo => sub { my $orig = shift; my $self = shift; my $value = $self->$orig(@_); return 'true' eq $value ? 'probably' : $value }; __PACKAGE__

    To test the role, just create its consumer:

    #!/usr/bin/perl { package ConsumerOf::MyRole; use Moo; with 'MyRole'; } use Test::Spec; use Test::Exception; describe MyRole => sub { my $o; before each => sub { $o = 'ConsumerOf::MyRole'->new; }; it 'knows the foo method' => sub { lives_ok { $o->foo }; }; it 'foos truth' => sub { is $o->foo(1), 'true'; }; it 'foos false' => sub { is $o->foo(0), 'false'; }; }; runtests();

    To test the class, just test its behaviour. The fact that it wraps the role is an implementation detail.

    #! /usr/bin/perl use Test::Spec; use MyClass; describe MyClass => sub { my $o; before each => sub { $o = 'MyClass'->new; }; it 'instantiates' => sub { isa_ok $o, 'MyClass'; }; it 'returns false' => sub { is $o->foo(0), 'false'; }; it "isn't sure" => sub { is $o->foo(1), 'probably'; }; }; runtests();

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I was able to mock the role, but it's not nice. It seems tools for handling roles in tests aren't there, yet (i.e. there's no does_ok ).
      #! /usr/bin/perl use Test::Spec; require MyClass; # No use! my $role_foo; { package MyRole; use Moo::Role; sub foo { $role_foo } } describe MyClass => sub { my $o; before each => sub { $o = 'MyClass'->new; }; it 'instantiates' => sub { isa_ok $o, 'MyClass'; }; it 'returns false' => sub { $role_foo = 'false'; is $o->foo('whatever'), 'false'; }; it "isn't sure" => sub { $role_foo = 'true'; is $o->foo('whatever'), 'probably'; }; }; runtests();

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        That's not working for me either, and it seems like it should. Here's the sketch I'm using:

        MyRole.pm

        package MyRole; use Moo::Role; sub foo { return 'bar'; } 1;

        rolesketch.pl

        use 5.016; package MyClass; use Moo; use FindBin qw($Bin); use lib $Bin; with 'MyRole'; around foo => sub { my ($orig, $self) = @_; return 'Wrapped ' . $self->$orig; }; package main; package MyRole { sub foo { return 'baz' }; }; my $obj = MyClass->new; my $res = $obj->foo; if ($res =~ /baz$/) { say qq{OK, got $res}; } else { say qq{Not OK, got $res}; }

      No, I want to test a behavior that is defined in around(), so I want to mock MyRole::foo to return something specific. I've already tested my class and my role separately. I want to test their interaction.

      Here's another example, a little closer to my actual code (which I can't post):

      package MyRole2 use Moo::Role; sub call { my $self = shift; # Connect to server, retrieve a document my $document = $self->get_document; return $document; } package MyClass2; use Moo; with 'MyRole2'; around call = sub { my ($orig, $self) = @_; my $document = $self->$orig; if (has_error($document)) { die 'Error'; } return parse($document); };

      So what I want to do here is to mock MyRole2::call to return a static document, defined in my test fixtures, that contains errors and test that the exception is thrown properly. I know how to test it using Test::More::throws_ok or similar. What I don't know how to do is to mock MyRole2::call and not MyClass2::call.

Re: Mocking a method defined in a Moo Role
by gnosti (Chaplain) on Jun 23, 2016 at 18:22 UTC
    I don't have a precise answer, but saw Chad Granum's YAPC 2016 presentation on Test2, and saw there are some flexible tools for mocking that may be worth investigating. Chad showed a link to the Test2 manual, but it was too fine a font to read on the video. Perhaps someone else can include it.
      use strict; use warnings; package MyRole; use Moo::Role; sub call { my $self = shift; return undef; } package MyClass; use Moo; with 'MyRole'; around call => sub { my ($orig, $self) = @_; my $document = $self->$orig; unless ($document) { die 'Error'; } return $document; }; package main; use Test::More; no strict 'refs'; no warnings qw/redefine once/; *MyClass::call = sub { "tada!" }; ok my $class = MyClass->new(); ok $class->call(); done_testing;
        Which part of the code mocks the MyRole's call ?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,