Solved

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

Posted on 2007-04-10
2
180 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:Hanqian
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 18885701
$module->mock('test2', sub{ $sub2_call=1; return {a=>1} } );
0
 

Author Comment

by:Hanqian
ID: 18891695

 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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question