#!/usr/bin/perl use Your::Module; use Proc::Background; use Test::Resub qw(resub); # disable Proc::Background->new for the scope of this block { my $fake_new = sub { return bless {}, 'Proc::Background'; # or whatever }; my $resub = resub 'Proc::Background::new', $fake_new; # continue on your merry way, content knowing that # your $fake_new is getting called. } # scope of $resub object ends, its DESTROYER restores # the original Proc::Background::new. #### package My::Module; use IO::Socket::INET; ... sub open_socket_to { my ($class, %args) = @_; return IO::Socket::INET->new( PeerAddr => $args{machine}, PeerPort => $args{port}, Proto => $args{protocol} || 'tcp', Type => SOCK_STREAM, ); } #### use Test; ... use My::Module; use Test::Resub qw(resub); use IO::Socket::INET; # test open_socket_to { my $rs = resub 'IO::Socket::INET::new', sub { 'a socket' }, capture => 1; my $socket = My::Module->open_socket_to( machine => 'example.com', port => '-29', ); # return value of ->open_socket_to is whatever IO::Socket::INET::new gives us ok( $socket, 'a socket' ); # We created the socket correctly - the fact that we provided an # illegal PeerPort doesn't need to be handled by this test: # IO::Socket::INET should have his own tests for his ->new. is_deeply( $rs->named_method_args, [{ PeerAddr => 'example.com', PeerPort => -29, Proto => 'tcp', Type => SOCK_STREAM, }] ); }