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;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.