Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stub and mock a subrotine for unit testing in perl

Tags:

mocking

stub

perl

I am trying to do unit testing of the module. I need help and information , how to mock or stub a subroutine to test package.

I do not wish to use the module I came across on cpan.

like image 685
made_in_india Avatar asked Dec 05 '25 16:12

made_in_india


1 Answers

You an mock subs by overriding them in the following way in your test:

no warnings;
local *Foo::bar = sub {
    # do stuff
};
use warnings;

You typically want to set a variable to be checked later in your test inside your mock.

(even if I suggest using Test::MockModule, but you explicitly specified to not use it)

like image 66
Karsten S. Avatar answered Dec 07 '25 16:12

Karsten S.