Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Test::More usage to test functions that die

by trammell (Priest)
on Dec 08, 2004 at 14:55 UTC ( #413210=note: print w/replies, xml ) Need Help??


in reply to Test::More usage to test functions that die

One way is to overload croak() inside your test, then make sure that calling your function calls croak() under the right circumstances. E.g.:
# (pseudocode) our $croakcount; sub main::croak { $croakcount++; } myfunc(my $badoptions); is ($croakcount, 1, "myfunc() calls croak()");

Replies are listed 'Best First'.
Re^2: Test::More usage to test functions that die
by stvn (Monsignor) on Dec 08, 2004 at 15:21 UTC

    This wont work unless all your code is in main:: (and even then its a delicate situation at best)

    #!/usr/bin/perl sub main::croak { print "I croaked"; } { package Test; use Carp; sub myfunc { croak("I am dying"); } } eval { Test::myfunc(); }; print $@ if $@; *myfunc = \&Test::myfunc; eval { myfunc(); }; print $@ if $@; __OUTPUT__ I am dying at test.pl line 18 I am dying at test.pl line 26
    However, what you are talking about is not unlike the ideas behind MockObjects, see Test::MockObject and Test::MockModule for more info.

    -stvn

      I know that the code as written won't work, which is why I labeled it pseudocode. But thanks for clarifying.

      Yes, this is essentially the MockObject strategy. Thanks for adding the links.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-01-24 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?