Test::Class::Moose::Role::AutoUse - Automatically load the classes you're testing
0.06
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 }
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.
use
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.
::
Tests::Some::Person
Some::Person
IHateTestingThis::Person
Person
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.
Test::
The example from our synopsis looks like this:
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.
Test::Class::Moose::Role::AutoUse
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:
Person::Employee
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 classname (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.
TestsFor::Person::Employee
TestsFor::Person-
TestsFor::Person::Employee-
TestsFor::Person
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?
Curtis "Ovid" Poe, <ovid at cpan.org>
<ovid at cpan.org>
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.
bug-test-class-moose at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Test::Class::Moose
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Class-Moose
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Test-Class-Moose
CPAN Ratings
http://cpanratings.perl.org/d/Test-Class-Moose
Search CPAN
http://search.cpan.org/dist/Test-Class-Moose/
Copyright 2012 Curtis "Ovid" Poe.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Test::Class::Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Class::Moose
CPAN shell
perl -MCPAN -e shell install Test::Class::Moose
For more information on module installation, please visit the detailed CPAN module installation guide.