Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now