In the startup method of my Test::Class base class for my tests, I have the following code:
# the class can be overridden in a subclass
my $class = $test->_class;
unless ($class) {
$class = ref $test;
$class =~ s/^Tests:://;
$test->_class($class);
}
# snip some special case code for traits
$class->require or $test->FAIL_ALL("Could not require $class: $@");
So let's say I have a Customer class. Its corresponding test class is Tests::Customer (this is the only time I use a plural in a package name). This allows my code to automatically load the code I want to test. Today, I just realized I was being foolish by mocking up methods in my classes for every test class, so I just added this to my base class:
sub _mock_class {
my $test = shift;
return Test::MockModule->new( $test->_class );
}
Now every test class can easily get a mocked version of the class.