in reply to Mocking a method defined in a Moo Role
Here's my example (because yours had several problems, e.g. switched $self and $orig in 'around'):
package MyRole; use Moo::Role; sub foo { $_[1] ? 'true' : 'false'; } __PACKAGE__
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,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Mocking a method defined in a Moo Role
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 | |
|
Re^2: Mocking a method defined in a Moo Role
by PopeFelix (Beadle) on Jun 24, 2016 at 14:45 UTC |