Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Test::MockObject and imported functions

by Mutant (Priest)
on Mar 13, 2008 at 10:03 UTC ( [id://673932]=perlquestion: print w/replies, xml ) Need Help??

Mutant has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm looking for a way of mocking a function that has been imported, e.g.

use Foo qw(bar); bar(%params); # <-- I want to mock this

bar() may also be imported implicitly. I know about fake_module(), but the code above will try to call import() on the mock object I've created. Do I have to mock import and somehow hook it into Exporter to export the actual function I want to mock?

Replies are listed 'Best First'.
Re: Test::MockObject and imported functions
by chromatic (Archbishop) on Mar 13, 2008 at 18:06 UTC

    Test::MockObject isn't really the right module for this. It relies on method dispatch. Test::MockModule is likely better.

    You could mock import() if you like, but it'll be more work that way.

Re: Test::MockObject and imported functions
by smerritt (Initiate) on Mar 14, 2008 at 03:22 UTC
    You might consider Test::Resub. That would look something like this:
    my $handle = Test::Resub->new({ name => 'Foo::bar', code => sub { "mocked out behavior" }, }); # now do something that calls Foo::bar

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://673932]
Approved by Erez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found