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,

In reply to Re: Mocking a method defined in a Moo Role by choroba
in thread Mocking a method defined in a Moo Role by PopeFelix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.