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 => ( ... );
Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
  • Related: [How can I mock Moose objects?](http://stackoverflow.com/questions/1365578/how-can-i-mock-moose-objects) – Ether May 14 '10 at 19:28
  • 2
    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.) – jrockway May 15 '10 at 03:12
  • Ooh, T::S looks neat. Nice work there. Won't get away with it at $job, but.... – darch May 16 '10 at 00:01

4 Answers4

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.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    I guess it's no use to report this problem, since it has been reported almost three years ago: https://rt.cpan.org/Public/Bug/Display.html?id=27699 Thanks for your workaround, I'll add my comment to that bug report. – Nikolai Prokoschenko May 14 '10 at 20:25
  • 1
    Report it to Test::Most. It doesn't have to export everything from Test::Deep. – Schwern May 17 '10 at 02:59
5

You can squelch particular exports via (for example):

use Test::Deep '!blessed';
Ether
  • 53,118
  • 13
  • 86
  • 159
3

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

Ovid
  • 11,580
  • 9
  • 46
  • 76
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.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • 1
    For those who are looking, I currently have an alpha of [Test::Class::Moose on github](https://github.com/Ovid/test-class-moose). – Ovid Dec 28 '12 at 13:43
  • Test::Class::Moose has been on the CPAN for years now. I should have come along and mentioned that. – Ovid May 14 '15 at 14:26