Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

RFC Mocking an Accessor with Test::MockObject

by ait (Hermit)
on Aug 16, 2010 at 06:10 UTC ( #855206=perlmeditation: print w/replies, xml ) Need Help??

I wanted to write a tutorial for this but probably this needs some more work or maybe there is already a known a better way to do this. I was actually surprised that Test::MockObject had no apparent easy way to emulate accessors, at least it was not obvious from the pod

The thing is that when testing complex Moose stuff I found myself having to mock quite a few objects and their getters and setters so I came up with a simple method of using global variables in the test script and mocking the getters and setters to them.

Comments welcome to see if it's worth anything for a tutorial, or comments on how to better do this!

# Setup the mockery my $entity = Test::MockObject->new(); $entity->fake_module('foo::entity'); my $error = undef; $entity->mock('error',sub {shift; &mock_accessor_var(\$error,@_)}); my $reqbody = undef; $entity->mock('reqbody',sub {shift; &mock_accessor_var(\$reqbody,@_)}) +; # Do the tests $entity->reqbody('blah'); my $r = $reqproc->decode($entity); ok(!$r, 'Bad request expected to fail'); ok($entity->error eq 'XDOMERR', 'Got XDOMERR'); # the accessor to var mocker sub mock_accessor_var { my $var = shift; if(@_){ $$var = shift; } else{ return $$var; } }

Replies are listed 'Best First'.
Re: RFC Mocking an Accessor with Test::MockObject
by chromatic (Archbishop) on Aug 16, 2010 at 20:51 UTC

    If I were to write T::MO again, I'd write Test::MockObject::Extends first, then perhaps generalize to T::MO. The less you need to mock, the better your tested API and the more robust your tests.

    If you have to mock an accessor, you're probably mocking too much.

      Yeah well every case is different IMHO. This test code is for a specific component a relatively complex Catalyst Model (that is modeled as an aggregate), so I wanted to test very specific classes that receive complex objects and Catalyst Model instances as well. Creating the actual objects would have been a real pain and I wanted to write unit tests for every single component. I looked into Test::MockObject::Extends but it was more than I needed (and virtually impossible and useless to instantiate the whole model to test such a small class), so I settled for the solution above.

      Do you think it's worth a tutorial? Is the title appropriate? I could start the article with your caveat above ;-) ("If you have to mock an accessor, you're probably mocking too much.")

        I'm always interested in more testing articles, especially for sites like

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://855206]
Approved by Corion
Front-paged by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2020-10-22 03:42 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (225 votes). Check out past polls.