如何将 Moose 与 Test::Class 一起使用?

发布于 2024-09-01 13:17:10 字数 537 浏览 3 评论 0原文

我目前正在重构由同事构建的测试套件,并希望在此过程中使用 Test::Class[::Most] 。当我开始时,我发现我真的可以使用几个 Moose 角色来稍微解耦代码。然而,这似乎不太可能——我收到这样的错误消息:

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

所以问题是:我可以将 MooseTest::Class 一起使用吗如果是这样,怎么办?

PS:代码是这样的:

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

with 'My::Cool::Role';

has attr => ( ... );

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 => ( ... );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再浓的妆也掩不了殇 2024-09-08 13:17:10

Test::Deep(通过 Test::Class::Most 通过 Test::Most 加载)正在导出自己的 blessed 以及许多其他可能不应该的东西。它没有记录。 Moose 还导出了更常见的 Scalar::Util::blessed。由于 Scalar::Util::blessed 相当常见,Test::Deep 不应该导出自己不同的 blessed

不幸的是,没有什么好办法来阻止它。我建议在 My::Test::Class::Base 中执行以下 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;

并将问题报告给 测试::深测试::大多数

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.

箜明 2024-09-08 13:17:10

您可以通过(例如)压制特定的导出:

use Test::Deep '!blessed';

You can squelch particular exports via (for example):

use Test::Deep '!blessed';
手长情犹 2024-09-08 13:17:10

我刚刚发布了 Test::Most 的更新版本。如果您安装 0.30,这个问题就会消失。

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

我不是你的备胎 2024-09-08 13:17:10

找到此页面的人们可能也有兴趣了解各种 Test::Class-Moose 混搭模块:

对于其中任何一个都需要进行一定程度的重构-- 语法有所不同。然而,通过一定量的查找和替换,您可能能够进行相当快速的转换。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文