How can I use Moose with Test::Class?
Asked Answered
K

4

8

I'm currently refactoring a test suite built up by a colleague and would like to use Test::Class[::Most] while doing so. As I started I figured out I could really use a couple of Moose roles to decouple code a little bit. However, it seems it's not quite possible -- I'm getting error messages like this one:

Prototype mismatch: sub My::Test::Class::Base::blessed: none vs ($) at
/usr/lib/perl5/vendor_perl/5.8.8/Sub/Exporter.pm line 896

So the question is: can I use Moose together with Test::Class and if so, how?

PS: The code goes like this:

package My::Test::Class::Base;
use Moose;
use Test::Class::Most;

with 'My::Cool::Role';

has attr => ( ... );
Kwabena answered 14/5, 2010 at 17:45 Comment(3)
Related: How can I mock Moose objects?Teeters
There is always Test::Sweet, which is a Moose-based OO testing framework. (It's not xUnit, because it uses Moose's existing concepts instead of inventing its own.)Huntington
Ooh, T::S looks neat. Nice work there. Won't get away with it at $job, but....Mycenaean
S
12

Test::Deep (loaded via Test::Most via Test::Class::Most) is exporting its own blessed along with a lot of other stuff it probably shouldn't be. Its not documented. Moose is also exporting the more common Scalar::Util::blessed. Since Scalar::Util::blessed is fairly common, Test::Deep should not be exporting its own different blessed.

Unfortunately, there's no good way to stop it. I'd suggest in My::Test::Class::Base doing the following hack:

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

# Test::Class::Most exports Test::Most exports Test::Deep which exports
# an undocumented blessed() which clashes with Moose's blessed().
BEGIN {
    require Test::Deep;
    @Test::Deep::EXPORT = grep { $_ ne 'blessed' } @Test::Deep::EXPORT;
}

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

and reporting the problem to Test::Deep and Test::Most.

Stratification answered 14/5, 2010 at 19:29 Comment(2)
I guess it's no use to report this problem, since it has been reported almost three years ago: rt.cpan.org/Public/Bug/Display.html?id=27699 Thanks for your workaround, I'll add my comment to that bug report.Kwabena
Report it to Test::Most. It doesn't have to export everything from Test::Deep.Stratification
T
5

You can squelch particular exports via (for example):

use Test::Deep '!blessed';
Teeters answered 6/2, 2012 at 19:12 Comment(0)
L
3

I've just released an updated version of Test::Most. If you install 0.30, this issue goes away.

Liking answered 5/9, 2012 at 9:36 Comment(0)
K
1

Folks finding this page might also be interested to know about the various Test::Class-Moose mashup modules:

With any of these some amount of refactoring would required-- the syntax varies. HOwever, with some amount of find-and-replace you may be able to make a fairly quick transition.

Kiakiah answered 14/5, 2010 at 17:46 Comment(2)
For those who are looking, I currently have an alpha of Test::Class::Moose on github.Liking
Test::Class::Moose has been on the CPAN for years now. I should have come along and mentioned that.Liking

© 2022 - 2024 — McMap. All rights reserved.