Solved

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

Posted on 2007-04-10
2
178 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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