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.

In reply to Mocking IO::Socket::INET by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.