Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Silencing warnings when testing

by Ovid (Cardinal)
on Oct 28, 2002 at 02:07 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-04-18 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found