Naming issue with Perl Sub::Override
perl, unit-testing
Solution
I presume `sub_b` is imported by the package that contains `sub_a`?
You're changing the sub to which the name `package_of_sub_b::sub_b` refers.
You're not changing the sub to which the name `package_of_sub_a::sub_b` refers.
If the second name you're resolving, so you need to override the `sub_b` in the package that contains `sub_a`.
override_sub( 'package_of_sub_a::sub_b', ... );
Problem
I am using the module Sub::Override to mock a subroutine during testing. I am actually testing a subroutine named sub_a. sub_a is calling another subroutine sub_b to do some work. sub_b is actually executing some commands over the ssh connection which I want to mock in my testing mode. So I am using sub::override to mock sub_b. ``` sub_a{ ... sub_b(arg1, arg2) } ``` In my test code I am overriding it like this ``` my $override_sshCommand = override_sub( 'package::filename::sub_b', sub ($$) { return "success"; }, undef ); ``` In the above code if sub_b and sub_a are in the same class then the override is successful. Otherwise the override does not work. I can fix this issue by using the fully qualified name of sub_b when calling from sub_a ``` sub_a{ ... package::filename::sub_b(arg1, arg2); } ``` The above code fixes the issue and the override is successful. But I don't want to do it like this, as I don't own that code and also it looks ugly. Is there any other solution ?