Search code examples
perlmoose

Using Moose with Test::Class - problems with constructor


I'm refactoring a test suite built on Test::Class, and would like to take advantage of such Moose niceties as Roles, both in the base test class, and in some of the test classes

I have tried:

  • Using MooseX::NonMoose in the base test class
  • Using MooseX::InsideOut in the base class
  • Setting make_immutable( inline_constructor => 0 );

Though I have never used either of those MooseX modules before and cannot be sure I used them correctly. In all my trials I have received the following error on running the test suite:

Test::Class internals seem confused. Did you override new() in a sub-class or via multiple inheritence?

Simple sample which produces this failure:

Base test class:

package My::Test::Class::Base;

use Moose;
use Test::Class::Most;

1;

A test class:

package Test::Package::Class;

use Moose;
use Test::Class::Most parent => 'My::Test::Class::Base';

sub blah : Tests() {
    my $test = shift;
    can_ok($test->class(), 'blah');
}

1;

Class being tested:

package Package::Class;

use Moose;

sub blah {
    my $self = shift;
    return 1;
}

1;

So, what am I doing wrong?


Solution

  • The problem here is that Test::Class doesn't want you to override its new() constructor, but Moose does just that. People keep asking me how to deal with this, so I recently wrote Test::Class::Moose. It's currently only on Github because it's alpha, but it seems fairly reasonable. Note that like Test::Class::Most, the common testing functions are provided for you.

    If you try it, please let me know and provide feedback so I can better know what people want.

    If you want something on the CPAN, there are several alternatives.

    It uses MooseX::Declare. Many people don't like that.

    You might not find the syntax intuitive, but it seems like a fine module.

    Same issue as Test::Routine.