NAME

Test::Class::Moose::Role::AutoUse - Automatically load the classes you're testing

VERSION

version 0.67

SYNOPSIS

package TestsFor::Some::Class;
use Test::Class::Moose;
with 'Test::Class::Moose::Role::AutoUse';

sub test_constructor {
    my $test  = shift;

    my $class = $test->class_name;             # Some::Class
    can_ok $class, 'new';                      # Some::Class is already loaded
    isa_ok my $object = $class->new, $class;   # and can be used as normal
}

DESCRIPTION

This role allows you to automatically use the classes your test class is testing, providing the name of the class via the class_name attribute. Thus, you don't need to hardcode your class names.

PROVIDES

class_name

Returns the name of the class you're testing. As a side-effect, the first time it's called it will attempt to use the class being tested.

get_class_name_to_use

This method strips the leading section of the package name, up to and including the first ::, and returns the rest of the name as the name of the class being tested. For example, if your test class is named Tests::Some::Person, the name Some::Person is returned as the name of the class to use and test. If your test class is named IHateTestingThis::Person, then Person is the name of the class to be used and tested.

If you don't like how the name is calculated, you can override this method in your code.

Warning: Don't use Test:: as a prefix. There are already plenty of modules in that namespace and you could accidentally cause a collision.

RATIONALE

The example from our synopsis looks like this:

package TestsFor::Some::Class;
use Test::Class::Moose;
with 'Test::Class::Moose::Role::AutoUse';

sub test_constructor {
    my $test  = shift;

    my $class = $test->class_name;             # Some::Class
    can_ok $class, 'new';                      # Some::Class is already loaded
    isa_ok my $object = $class->new, $class;   # and can be used as normal
}

Without this role, it would often look like this:

package TestsFor::Some::Class;
use Test::Class::Moose;
use Some::Class;

sub test_constructor {
    my $test  = shift;

    can_ok 'Some::Class', 'new';
    isa_ok my $object = 'Some::Class'->new, 'Some::Class';
}

That's OK, but there are a couple of issues here.

First, if you need to rename your class, you must change this name repeatedly. With Test::Class::Moose::Role::AutoUse, you only rename the test class name to correspond to the new class name and you're done.

The first problem is not very serious, but the second problem is. Let's say you have a Person class and then you create a Person::Employee subclass. Your test subclass might look like this:

package TestsFor::Person::Employee;

use Test::Class::Moose extends => "TestsFor::Person";

# insert tests here

Object-oriented tests inherit their parent class tests. Thus, TestsFor::Person::Employee will inherit the TestsFor::Person->test_constructor() method. Except as you can see in our example above, we've hardcoded the class name, meaning that we won't be testing our code appropriately. The code using the Test::Class::Moose::Role::AutoUse role doesn't hardcode the class name (at least, it shouldn't), so when we call the inherited TestsFor::Person::Employee->test_constructor() method, it constructs a TestsFor::Person::Employee object, not a TestsFor::Person object.

Some might argue that this is a strawman and we should have done this:

package TestsFor::Some::Class;
use Test::Class::Moose;
use Some::Class;

sub class_name { 'Some::Class' }

sub test_constructor {
    my $test  = shift;

    my $class = $test->class_name;             # Some::Class
    can_ok $class, 'new';                      # Some::Class is already loaded
    isa_ok my $object = $class->new, $class;   # and can be used as normal
}

Yes, that's correct. We should have done this, except that now it's almost identical to the AutoUse code, except that the first time you forget to use the class in question, you'll be unhappy. Why not automate this?

BUGS

Please report any bugs or feature requests to bug-test-class-moose at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Class-Moose. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Test::Class::Moose

You can also look for information at:

AUTHORS

  • Curtis "Ovid" Poe <ovid@cpan.org>

  • Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 - 2015 by Curtis "Ovid" Poe.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.