Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Silencing warnings when testing

by Ovid (Cardinal)
on Oct 28, 2002 at 02:07 UTC ( #208432=note: print w/ replies, xml ) Need Help??


in reply to Silencing warnings when testing

I typically use typeglobs to override the sub and capture the arguments. Then, you can test whether or not carp was called and if it received the correct arguments. Below is a test script.

#!/usr/bin/perl -w use strict; package Foo; use Carp; sub bar { carp "This is a test"; } package main; use Test::More 'no_plan'; use constant MODULE => 'Foo'; { my $carp; local *Foo::carp = sub { $carp = join '', @_ }; can_ok( MODULE, 'bar' ); Foo::bar(); ok( $carp, 'Calling Foo::bar() should call carp()' ); is( $carp, 'This is a test', '... with the correct error message' ); }

Just make sure that you localize the typeglob. This ensures that subsequent calls to carp that you forgot to trap will still spit out error messages.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


Comment on Re: Silencing warnings when testing
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-10-25 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls