Link to home
Start Free TrialLog in
Avatar of Hanqian
Hanqian

asked on

How to fake the parameter of subroutine called in another subroutnie?

Dear expert,

I am doing a perl testing using Test::MockModule. i just give a simple example here:

I have perl module test.pm
 package Test;
   
    sub test1 {
       my $result_ref = test2();

      if ( $result_ref ) {
          test3( $result_ref->{a} );
       }

    }
    sub test2  {
      my $result_ref;
     ..........
      return $result_ref
   }
   sub test3 {
   }

In my test file: test.t
I need to test if subroutines test2 and test3 are called in sub test1.
so I mock test2 and test3.
my $module = new Test::MockModule( 'test' );
my ($sub2_call, $sub3_call);
$module->mock('test2', sub{ $sub2_call=1} );
$module->mock('test3', sub{ $sub3_call=1} );

I need to test when mocked test2 returns 1, then test3 is called. But when I test it always has the problem with the parameter "$result_ref->{a}" since the real sub test2 is not called and $result_ref->{a} doesn't exist. My question is how to fake (mock) the parameter of sub. test3.

 Thanks.
Hanqian
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hanqian
Hanqian

ASKER


 I am not really clear about :  sub{ $sub2_call=1; return {a=>1} },  

if the sub returns {a=>1}. What does $sub2_call = 1 mean?

 I thought that $sub2_call = 1  is to mock the real test2 to  sub  test2 {return 1} ,  to see if  test2 is called by:  is($sub2_call, 1, ' test2 is called');

In your case, how to test if test2 is called, we can't use  is($sub2_call, 1, ' test2 is called'); ?

This is the my first perl testing program, I appreciate your help.
 
Thanks
Hanqian