Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by MARKWIN (Novice)
on Jun 18, 2021 at 16:16 UTC ( [id://11134012]=note: print w/replies, xml ) Need Help??


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

I had not come accross this module before. It looks good, but doesn't solve the ultimate problem as far as I can see.

My test code does not call Rest::Client, which is what I need to mock. Rest::Client is used by the module under test, not the test code.

To test the module under test I do not expect to change it!

I tweak the INC path so that when the module under test loads Rest::Client it gets my mock version.

I already have a very good mock, one that does exactly what I need and works well. I want the innocent and unchanged use line in the module under test to load the same-named mock module rather than the real one.

The problem I have is that I cannot include the mock module in my module due to a name space violation, after all I did not write Rest::Client so I cannot publish it, even as a sub module.

How can I publish this module without violating the real owners name space? Is there a way to include a module as source code without exporting that module?

  • Comment on Re^2: How to use a private version of a published module?

Replies are listed 'Best First'.
Re^3: How to use a private version of a published module?
by hippo (Bishop) on Jun 18, 2021 at 16:52 UTC
    My test code does not call Rest::Client, which is what I need to mock. Rest::Client is used by the module under test, not the test code.

    It does not matter where in the chain of calls REST::Client is used. It is the subs within REST::Client which are mocked. You do not need to change your module at all. Your test script sets up the mocking, instantiates an object of your class and when your test script calls some method of your class which, somewhere down the line, calls a method on REST::Client, that is what is magically mocked by Test::MockModule.

    Trust me on this, mocking is definitely the way to go here. You don't have to use Test::MockModule - there are plenty of other good mocking modules. The very fact that there are many of them should hopefully indicate to you how prevalent mocking is as a valid test tool and to get around precisely the problem you are presently having.


    🦛

      The solution that worked, that used the module I had already written was
      use Package::Alias 'REST::Client'=>'Finance::IG::REST::Client' ;
      And a rename of my mock REST::Client into my own namespace. I could have called it MockREST::Client or anything really. So now there is no REST::Client in my heirachy, only Finance::IG::REST::Client which is in my namespace, but calling this before the module under test is used, means the module under test uses the mocked version not the real thing.

      This means there is no name space permission problem when I upload to CPAN.

      Thanks very much for everyones help.

Log In?
Username:
Password:

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

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

    No recent polls found