Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

How to test a Moose Role?

by nbezzala (Scribe)
on Aug 05, 2011 at 17:54 UTC ( #918837=perlquestion: print w/replies, xml ) Need Help??
nbezzala has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to write tests for just the Moose Role? Or do I have to write tests for all the classes which consume the role?

Replies are listed 'Best First'.
Re: How to test a Moose Role?
by chromatic (Archbishop) on Aug 05, 2011 at 17:59 UTC

    Which works best for you?

    I've had success writing a minimal test class which consumes the role and testing that, but other times I've had more success testing the actual classes which consume the role. My working guide seems to be that if I consume the role in one or two places, testing the actual classes works better. If I consume the role in many places, a test consumer works better.

    I suspect the answer has something to do with how specific or generic the role is.

Re: How to test a Moose Role?
by Nomad (Pilgrim) on Aug 08, 2011 at 12:11 UTC

    One way is to write a minimal class that consumes the role and then write tests against the class.

    Also, you might want to test your consuming classes anyway, because, you need to make sure they are consumed in the way you expect, and you might have different return values or arguments that need to be tested. For example, I have a project where two different classes consume a role. One class returns 'undef' on failure and another has to, because of what the class actually does, return '' on failure. These situations are similar but different and need to be tested appropriately.

Re: How to test a Moose Role?
by jandrew (Chaplain) on Mar 30, 2012 at 19:19 UTC

    Since I keep coming back to this node when I do a Super Search for this question I thought this might be a good place to add a new possible solution

    use Test::Most; use Test::Moose; use MooseX::ClassCompositor; use MyCoolNewRole v1.0;#To test for version availability my @attributes = qw( first_attribute ); my @methods = qw( important_method ); my ( $instance ); my $class = MooseX::ClassCompositor->new({ class_basename => 'Test', })->class_for( 'MyCoolNewRole', ); map has_attribute_ok( $class, $_ ), @attributes; map can_ok( $class, $_ ), @methods; lives_ok{ $instance = $class->new( first_attribute => 'cool', )} 'Test creation of an instance'; # other cool tests, for example; # is( 42, $instance->important_method( '?' ), 'The answer') ) done_testing();

    This solution was not available on CPAN at the time of the original post. MooseX::ClassCompositor++

Re: How to test a Moose Role?
by grubert (Initiate) on Jul 15, 2014 at 12:57 UTC
    I usually just add a small test package at the end of the role, ex:
    package Foo::Role::Bar; use Moose::Role; < ...role implementation code...> package Foo::Role::Bar::Test; use Moose; with 'Foo::Role::Bar'; 1;
    Then I test against the test class.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://918837]
Approved by toolic
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-08-23 08:37 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (350 votes). Check out past polls.