wanna_code_perl has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a protocol implementation for which the previous maintainer wrote no unit tests whatsoever. To get any reasonable amount of coverage, I need to mock IO::Socket::INET. The actual mocked routines will be easy: mainly just scripted send()/recv() sequences with a few refused connections, timeouts and disconnects thrown in for good measure. The unexpectedly hard part seems to be actually mocking IO::Socket::INET in a useful way.

Since IO::Socket::INET->new() actually initiates a connection, and the connection handshaking for my protocol is important to test (so I can't just skip it and shove in my own object in place of a connected socket), I need to mock new() as well. So, I went for Test::MockModule, but the methods I've mocked appear to be nonexistent ("Can't locate object method..."). Not even calling the original method.

I've boiled down my efforts into a single source file (below). The idea is that I'm able to call mock_me() (normally from an included lib/helper.pl file) with custom subs for IO::Socket::INET, but it returns an instance of My::Module. The real code has more logic to do custom/conditional send()/recv() chains and the like, but the code below illustrates the idea, and more importantly, the problem. Anything much more complex than the examples given in Test::MockModule's synopsis seems to fail, even in the same scope, or passing scalar refs around.

How do I make a sensible mocked up framework for IO::Socket::INET? As it stands now, I'm leaning towards forgetting about Test::MockModule and just doing my own thing, but I'd rather believe this is a solved problem and I'm just doing it wrong.

Not that it matters for this example, but my real code uses IO::Socket::INET entirely with the OO interface. No <$socket> mumbo-jumbo.

#!perl -T use 5.010; use strict; use warnings; ## My::Module - Module I'm writing unit tests for package My::Module; use Carp; use IO::Socket 1.18; sub new { my $class = shift; bless { }, $class } sub connect { my $s = shift; $s->{socket} = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 8080, Proto => 'tcp', ) or croak "Connection failed: $!"; croak 'Invalid handshake' unless $s->{socket}->recv eq 'Hiya!'; $s->{socket}->send('Howdy!'); croak 'ACK barf' unless $s->{socket}->recv eq 'Welcome'; 1; } ## main - Reasonable facsimile for t/02-connect.t package main; use Test::More; use Test::MockModule; my $obj = mock_me( send => sub { $_[0]->{howdy} = 1 if $_[1] eq 'Howdy!' }, recv => sub { $_[0]->{howdy} ? 'Welcome' : 'Hiya!' }, ); is 'My::Module', ref $obj, 'Correct module'; lives_ok { $obj->connect } 'Connection and handshake success'; done_testing; # Mock for IO::Socket::INET with the provided overrides sub mock_me { my $mock = Test::MockModule->new('IO::Socket::INET'); my %mock = ( new => sub { $mock }, send => sub { warn "send($_[0])" }, recv => sub { warn "recv" }, @_ ); $mock->mock($_ => $mock{$_}) for keys %mock; My::Module->new; } __END__ ok 1 - Correct module Can't locate object method "recv" via package "Test::MockModule" at te +st.pl line 24. # Tests were run but no plan was declared and done_testing() was not s +een. # Looks like your test exited with 255 just after 1.

Replies are listed 'Best First'.
Re: Mocking IO::Socket::INET
by choroba (Cardinal) on Aug 31, 2019 at 15:42 UTC
    There are two problems:
    1. You return $mock from IO::Socket::INET->new, but that's an object responsible for mocking the class, not an object pretending to be IO:Socket::INET. I'm not familiar with Test::MockObject, but it seems it doesn't provide an easy way to create the mocked object, so you probably have to go low level and create the object yourself:
      new => sub { bless {}, 'IO::Socket::INET' },
    2. The methods of IO::Socket::INET remain mocked only while the $mock object exists. Once you don't return it from the constructor, it goes out of scope at the end of mock_me and IO::Socket::INET will behave in its original way. You need to return the $mock object from there to the test so IO::Socket::INET stays mocked.

    This works for me:

    #!/usr/bin/perl use warnings; use strict; { package main; use Test::More tests => 2; use Test::Exception; use Test::MockModule; my ($obj, $mock) = mock_me( # Keeping the $mock object. send => sub { $_[0]->{howdy} = 1 if $_[1] eq 'Howdy!' }, recv => sub { $_[0]->{howdy} ? 'Welcome' : 'Hiya!' }, ); is 'My::Module', ref $obj, 'Correct module'; lives_ok { $obj->connect } 'Connection and handshake success'; sub mock_me { my $mock = Test::MockModule->new('IO::Socket::INET'); my %mock = ( # Initializing the object. new => sub { bless {}, 'IO::Socket::INET' }, @_ ); $mock->mock($_ => $mock{$_}) for keys %mock; return My::Module->new, $mock # Returning the $mock object. } } { package My::Module; use Carp; use IO::Socket 1.18; sub new { my $class = shift; bless { }, $class } sub connect { my $s = shift; $s->{socket} = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 8080, Proto => 'tcp', ) or croak "Connection failed: $!"; croak 'Invalid handshake' unless $s->{socket}->recv eq 'Hiya!' +; $s->{socket}->send('Howdy!'); croak 'ACK barf' unless $s->{socket}->recv eq 'Welcome'; 1; } }

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      $choroba->eureka;

      That did the trick. Thank you very much.