I'm looking for a way to test a subroutine by mocking a subroutine it calls, using the perl module Test::MockModule.
Let's say we test a sub My::Module::A(). It calls the sub My::New::B(). In order to test My::Module::A(), I mock My::New::B(). My::New::B(), however, calls another sub My::Calc::C() to do some calculations. Its obligatory to call My::Calc::C() in the mocked sub.
my $module = Test::MockModule->new('My::New');
$module->mock( B => sub($$)
{
my ($first, $second) = @_;
My::Calc::C();
} );
My::Calc::C() needs to know who calls it, however as a caller it receives 'main::test' instead of 'My::New::B'. Is there a way to tell to My::Calc::C() that it's called by the mocked My::New::B() and not by main::test()?
The package keyword sets the current calling package. You can enclose it in braces to restrict it to a particular scope:
my $module = Test::MockModule->new('My::New');
$module->mock( B => sub($$)
{
my ($first, $second) = @_;
{
package My::New::B;
My::Calc::C();
}
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With