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

I am trying to write some testing code to mock Apache::Constants which is exporting some methods to the script. I know how to Mock a OO module, but not a module that is exporting just methods. This is the code I am mocking,

use Apache::Constants qw(OK DECLINED AUTH_REQUIRED); my $status = some_method(); return $status if ($status != OK);

I have tried this but this is not working so is probalbly just wrong.

$INC{Apache/Constants.pm} = 1; my $constants = Test::MockObject->new(); $constants->fake_module('Apache::Constants'); $constants->mock('OK', sub { return 1 } ); warn "OK is " . OK . "\n";

gives me "Undefined subroutine &Apache::Constants::OK called"

The reason I tried Test::MockObject is that the Apache::Constants is being used in the module that I am testing so I need to be able to export this mock object to that other module.

Replies are listed 'Best First'.
Re: Mocking Apache::Constants
by stvn (Monsignor) on May 25, 2006 at 20:18 UTC

    I doubt that Test::MockObject will handle exporting the constants for you. You might want to look at Test::MockModule which IIRC allows you to override module subroutines selectively, which might allow you to just override OK and nothing else (although I am not sure that is really what you are looking for here). Apache::Constants is an XS module, so it's internals may not be so easily manipulated.

    -stvn

      I was not really worried about Apache internals, I justed need for something to be returned when those functions are called.

      The Test::MockModule did do the trick, it took a little work but I have it completed tested now. Thanks for the help.

Re: Mocking Apache::Constants
by ForgotPasswordAgain (Vicar) on May 26, 2006 at 16:00 UTC