in reply to Re^2: Testing procedure: how to mock?
in thread Testing procedure: how to mock?

thor

Your problem here is that your Foo module has already exported &foo into Bar's namespace by the time you have mocked anything. What you want to do is to mock Foo before loading Bar, like this:

use strict; use warnings; use Test::MockModule; my $mock = Test::MockModule->new('Foo'); $mock->mock('foo', sub {print "Successfully mocked!\n";}); require Bar; my $bar = Bar->new(); $bar->bar();
Personally I don't think this is ideal since you need to require Bar rather than use it.

However, since you are exporting &Foo::foo into Bar, you could ignore the fact &Foo::foo came from Foo, and just mock &foo inside Bar. Like this:

use strict; use warnings; use Test::MockModule; use Bar; my $mock = Test::MockModule->new('Bar'); $mock->mock('foo', sub {print "Successfully mocked!\n";}); my $bar = Bar->new(); $bar->bar();
This too is probably not ideal.

Of course, if you simply do not export &Foo::foo, and instead call with the fully qualitied packag name (Foo::foo()) then you original test code will work.

-stvn