CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unit Test: Mocking a subroutine

 



saumrh
New User

Nov 19, 2015, 7:24 PM

Post #1 of 2 (1087 views)
Unit Test: Mocking a subroutine Can't Post

Hi,
I am learning to write unit test in perl using by mocking subroutines.
I have a subroutine which basically invokes connect subroutine belonging to Socket module. Based on reply back, I perform processing.
Connection.pm

Code
use Socket; 
sub connect_test {
my $host = shift;
my $full_address = shift;
my $return_value = connect($host, $full_address);
// Some processing

}


Connection.t

Code
my $socket_module = Test::MockModule->new('Socket'); 
$socket_module->mock( 'connect' => sub { return "true" });
$status = Connection->connect_test(127.0.0.1, 12);


This however still calls the provided host address and tests. I wanted to mock the connect so that irrespective of address passed it should return true.

In general the scripts are not OO. All online material indicate I am supposed to create a Object for socket.
If my understanding is correct, the mocked Socket object is never being used in my connect_test call.
Can someone please help me out in understanding what correction should I be doing?

Thank-you


Zhris
Enthusiast

Nov 23, 2015, 12:01 PM

Post #2 of 2 (1047 views)
Re: [saumrh] Unit Test: Mocking a subroutine [In reply to] Can't Post

Hi,


Quote
If my understanding is correct, the mocked Socket object is never being used in my connect_test call.


Thats also my understanding. Your options are to override the connect_test method instead, or create a wrapper connect method that simply passes the arguments directly onto Socket's connect method.

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives