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:
Personally I don't think this is ideal since you need to require Bar rather than use it.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();
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:
This too is probably not ideal.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();
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.
In reply to Re^3: Testing procedure: how to mock?
by stvn
in thread Testing procedure: how to mock?
by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |