如何模拟 Moose 对象?

发布于 2024-08-03 19:19:00 字数 296 浏览 6 评论 0原文

Perl 人们在模拟 Moose 对象时使用了哪些策略,并将其作为类型约束属性注入到其他 Moose 对象中?

Test::MockObject::Extends 似乎与 Moose 配合得不好。我需要将该对象祝福为特定的包,因此普通的 Test::MockObject 将无法工作。我相信其他人也遇到过类似的困难。你是怎么解决的?

已在 CPAN 上的解决方案可获得额外积分。

What strategies have Perl people used when mocking Moose objects that they will inject into other Moose objects as type-constrained attributes?

Test::MockObject::Extends doesn't seem to play well with Moose. I need the object to blessed as a specific package though so a vanilla Test::MockObject won't work. I'm sure other folks have had similar difficulty. How did you resolve it?

Extra Points for Solutions that are already on CPAN.

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

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

发布评论

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

评论(2

神经大条 2024-08-10 19:19:00

好吧,我不是此类事情的专家,但我首先要看的是 Shaw Moore 的 (Sartak) 测试MockOO

如果这对您不起作用,那么我会考虑使用元对象协议的强大功能并开始手动构建模拟对象。查看 Class::MOP::Class 和 Moose::Meta::Class 了解如何以编程方式在运行时重写特定方法和/或创建整个类。

如果这仍然不适合你,我会跳过 IRC 并询问。驼鹿重击手在那里闲逛,我确信他们中的一个也遇到过这种情况。

Well I'm not the expert on such things but the first thing I'd look at is Shaw Moore's (Sartak) Test-MockOO.

If this doesn't work for you, I'd then look at using the power of the Metaobject Protocol and starrt manually building Mock objects. Look at Class::MOP::Class and Moose::Meta::Class for how to override specific methods and/or create entire classes at runtime programatically.

If this still doesn't work for you, I'd swing past IRC and ask. The moose hevy hitters hang out there and I'm sure one of them has run into this situation.

泛滥成性 2024-08-10 19:19:00

有点自我插件,但我写了 http:// /search.cpan.org/~cycles/Test-Magpie-0.05/lib/Test/Magpie.pm,也许你会发现这很有用。用它创建的模拟可以充当任何类,并执行所有可能的角色。它根本不模拟特定的对象或类。遗憾的是,CPAN 的搜索有点垃圾,因此搜索“测试模拟”不会在结果中显示。

我还应该提到,该文档不包含大量的动机或示例代码,因此您可能希望检查一些测试:

bit of a self plug, but I wrote http://search.cpan.org/~cycles/Test-Magpie-0.05/lib/Test/Magpie.pm, maybe you'll find this useful. A mock created with this acts as any class, and does every role possible. It doesn't mock a specific object or class at all. Sadly CPAN's search is a bit rubbish, so searching for "test mock" doesn't show it in the results.

I should also mention that the documentation doesn't contain a huge amount of motivation or example code, so you may wish to check some of the tests:

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