Skip Menu |

This queue is for tickets about the Test-Mock-Simple CPAN distribution.

Report information
The Basics
Id: 95826
Status: resolved
Priority: 0/
Queue: Test-Mock-Simple

People
Owner: Nobody in particular
Requestors: TINITA [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Mocking does not work for inline packages
Hi, thanks for this nice and simple module. I have a tiny problem with mocking $dbh->quote. my $mock = Test::Mock::Simple->new(module => 'DBI::db'); => Can't locate DBI/db.pm in @INC DBI::db is not in its own file, but in DBI.pm. I don't have a good idea how to get around that exept checking if the namespace %DBI::db:: contains anything and in that case ignore the error message from require. What do you think? As a workaround I'm doing sub DBI::db::quote { ... }
Subject: Re: [rt.cpan.org #95826] Mocking does not work for inline packages
Date: Wed, 21 May 2014 08:30:19 -0700
To: bug-Test-Mock-Simple [...] rt.cpan.org
From: Erik Tank <tank [...] jundy.com>
There has to be a 'simple' work around for this; otherwise Perl wouldn't be able to find it. I'll be looking into it later tonight and hopefully will have a fix up soon. Thanks, Erik On 05/21/2014 02:30 AM, Tina Muller via RT wrote: Show quoted text
> Wed May 21 05:30:43 2014: Request 95826 was acted upon. > Transaction: Ticket created by TINITA > Queue: Test-Mock-Simple > Subject: Mocking does not work for inline packages > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: TINITA@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=95826 > > > > Hi, > thanks for this nice and simple module. > I have a tiny problem with mocking $dbh->quote. > > my $mock = Test::Mock::Simple->new(module => 'DBI::db'); > > => Can't locate DBI/db.pm in @INC > > DBI::db is not in its own file, but in DBI.pm. > > I don't have a good idea how to get around that exept checking if the namespace > %DBI::db:: contains anything and in that case ignore the error message from require. > > What do you think? > > As a workaround I'm doing sub DBI::db::quote { ... }
This is turning into quite the pickle. Packages that are contained within other packages (in-line) have a simple solution (which only works if the 2nd package is a subset of the main page - IE: DBI::db is found in DBI.pm). What makes what you're attempting to do even worse is that DBI::db is dynamically created - in a very conscience manner - it doesn't simple override the symbol table, but appears to add to it. This is making for some fun while I'm trying to find a good way around this. So currently I'm looking at 3 scenarios: 1) simple in-line packages where the 'sub' package is contained in the file of the main package which is contained in the same name space. This just needs some documented examples. 2) in-line packages which is not in the same name space as the 'main' package. This will need some more information provided in the new() call - not sure what I'd call it yet. Maybe 'parent_package'? 3) modules that muck around in the symbol table after the fact - which is what DBI seems to be doing. Not sure how to handle this yet. Thank you for finding this! Erik On Wed May 21 05:30:43 2014, TINITA wrote: Show quoted text
> Hi, > thanks for this nice and simple module. > I have a tiny problem with mocking $dbh->quote. > > my $mock = Test::Mock::Simple->new(module => 'DBI::db'); > > => Can't locate DBI/db.pm in @INC > > DBI::db is not in its own file, but in DBI.pm. > > I don't have a good idea how to get around that exept checking if the > namespace > %DBI::db:: contains anything and in that case ignore the error message > from require. > > What do you think? > > As a workaround I'm doing sub DBI::db::quote { ... }
0.09 has a new feature 'no_load'. Although I have not tested it for your particular circumstances (I'm working on some expanded tests for several different scenarios that I've been made aware of) it should work. If you are able to give it a try let me know. I will close this ticket once I've created a test that shows it works with inline packages. Erik
I'm closing this bug. Unfortunately, in-line packages (or packages/name-spaces that can't be located by converting the namespace into a file name) can not be handled by Test::Mock::Simple as it was initially designed. The initial idea was that you could simply mock out the parts that were causing you problems and then use the rest of the original code. Since 'in-line' packages can't be found in the normal manner you need to mock out the whole namespace. You can do this with Test::Mock::Simple, but there are other module that specialize in this that may be better suited for this. Wish I had better news - thanks for the bug report! Erik
Test::Mock::Simple is not a solution for attempting to (partially) mock namespaces which can not be found by the general package name to file name pattern.