Solved

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

Posted on 2007-04-10
2
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

737 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