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 | |
by choroba (Cardinal) on Jun 24, 2016 at 11:50 UTC | |
by PopeFelix (Beadle) on Jun 24, 2016 at 17:59 UTC | |
by PopeFelix (Beadle) on Jun 24, 2016 at 18:25 UTC | |
by choroba (Cardinal) on Jun 24, 2016 at 18:48 UTC | |
by PopeFelix (Beadle) on Jun 28, 2016 at 15:14 UTC | |
by PopeFelix (Beadle) on Jun 24, 2016 at 14:45 UTC | |
|
Re: Mocking a method defined in a Moo Role
by gnosti (Chaplain) on Jun 23, 2016 at 18:22 UTC | |
by Anonymous Monk on Jun 29, 2016 at 15:22 UTC | |
by choroba (Cardinal) on Jun 29, 2016 at 15:35 UTC |