Test::Class::Moose::Role::AutoUse - Automatically load the classes you're testing
version 0.92
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.
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.
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 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.
TestsFor::Person::Employee
TestsFor::Person->test_constructor()
TestsFor::Person::Employee->test_constructor()
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?
Bugs may be submitted at https://github.com/houseabsolute/test-class-moose/issues.
I am also usually active on IRC as 'autarch' on irc://irc.perl.org.
irc://irc.perl.org
The source code repository for Test-Class-Moose can be found at https://github.com/houseabsolute/test-class-moose.
Curtis "Ovid" Poe <ovid@cpan.org>
Dave Rolsky <autarch@urth.org>
This software is copyright (c) 2012 - 2017 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.
The full text of the license can be found in the LICENSE file included with this distribution.
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.