Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

what and how mocking test is?

by xiaoyafeng (Chaplain)
on Jul 17, 2012 at 05:48 UTC ( #982139=perlquestion: print w/replies, xml ) Need Help??
xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Recently, I've modified a module of mine and want to do some tests. But what's stuck me is this module should connect to a database for initials, communicate with a remote DCOM server and apparently my notebook can't afford them.

I've heard mocking test long before. So can I complete this by mock tech.? if the answer is yes, where should I start? Any tutorials? any modules on cpan I can learn from?

Thanks a lot!!!

I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: what and how mocking test is?
by Corion (Pope) on Jul 17, 2012 at 07:13 UTC

    Personally, I avoid mocking modules, because they add one more layer of indirection, and mocking up a single function isn't too hard:

    use Some::Module; use Test::More tests => 2; { my $some_function_called; local *some_function = sub { $some_function_called++; return @canned_results; }; my @res = other_function_that_calls_some_function(); is $some_function_called, 1, 'We asked for resource existence'; is_deeply \@res, ['expected', 'results'], 'We got good results'; }

    Of course, if you need to mock out large object functionality, one of the Mock modules might help you better, but I like to replace just the relevant parts of an object and have come to the conclusion that I best write my constructors and methods in a way such that I can pass in all "needed" objects or data as optional parameters. If they are not present, they get taken from elsewhere, but otherwise the passed-in values get used.

Re: what and how mocking test is?
by Khen1950fx (Canon) on Jul 17, 2012 at 06:38 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982139]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-26 17:32 GMT
Find Nodes?
    Voting Booth?