Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock Moose objects?

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.

like image 475
Jeremy Wall Avatar asked Sep 02 '09 02:09

Jeremy Wall


2 Answers

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.

like image 109
perigrin Avatar answered Oct 28 '22 10:10

perigrin


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:

  • http://cpansearch.perl.org/src/CYCLES/Test-Magpie-0.05/t/mockito_tutorial.t
  • http://cpansearch.perl.org/src/CYCLES/Test-Magpie-0.05/t/basic.t
like image 28
ocharles Avatar answered Oct 28 '22 10:10

ocharles