Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: How to use a private version of a published module?

by stevieb (Canon)
on Jun 18, 2021 at 18:22 UTC ( #11134014=note: print w/replies, xml ) Need Help??

in reply to How to use a private version of a published module?

Here's an example of mocking any subroutine in any module at any level of depth (using my Mock::Sub distribution).

Order of operation: <- <- <- REST::Client

So is our module we're testing with, can be someone else module used by our, and REST::Client is being used by the module which we have no control over.

package A; use lib '.'; use B; sub new { return bless {}, shift; } sub call_b { my ($self) = @_; my $b = B->new; return $b->call_rest_client; } 1;

package B; use REST::Client; sub new { return bless {}, shift; } sub call_rest_client { my ($self) = @_; my $client = REST::Client->new; $client->GET('adfdsaf'); return $client->responseContent; } 1;

use warnings; use strict; use feature 'say'; use lib '.'; use A; use Mock::Sub; my $obj = A->new; say "Before mocking REST::Client->responseContent:\n"; say $obj->call_b; # Mock the sub, and set a custom return value my $response_client_sub = Mock::Sub->new->mock('REST::Client::response +Content'); $response_client_sub->return_value('{"a": 1, "b": 2}'); say "After mocking REST::Client->responseContent:\n"; say $obj->call_b;


Before mocking REST::Client->responseContent: Can't connect to adfdsaf:80 (Name or service not known) Name or service not known at /home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/site_perl/5.26.1/LWP/Protocol/ line 50. After mocking REST::Client->responseContent: {"a": 1, "b": 2}

See the documentation for Mock::Sub for full details and usages.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134014]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2022-01-16 18:18 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (49 votes). Check out past polls.