Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Overriding a module that is used by a program I'm testing

by belden (Friar)
on Dec 09, 2010 at 22:06 UTC ( [id://876336]=note: print w/replies, xml ) Need Help??


in reply to Overriding a module that is used by a program I'm testing

Test::Resub handles this. We use this extensively in our test suites at work. In your test:
#!/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.
Probably you'd want to have a single resub ofProc::Background::new up high in your test, so you don't need to keep creating the resub.

Test::Resub allows you to capture the arguments that go to your resubbed code. For example, consider this helper function

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, ); }
Not only would it be nice to prevent the test from actually creating sockets, it would be good to know how we're creating those sockets. The test might look like this

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' }, captur +e => 1; my $socket = My::Module->open_socket_to( machine => 'example.com', port => '-29', ); # return value of ->open_socket_to is whatever IO::Socket::INET::n +ew 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, }] ); }
Sometimes you might find you want to allow the original code to run, but you want to eavesdrop in on the arguments that get sent in to it. Test::Wiretap handles that.

You can have multiple resubs and wiretaps in place for the same subroutine: they'll stack properly and unroll correctly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://876336]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-28 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found