Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Deliberate failure in Test::More

by macrobat (Beadle)
on Jun 07, 2010 at 01:32 UTC ( #843383=perlquestion: print w/replies, xml ) Need Help??

macrobat has asked for the wisdom of the Perl Monks concerning the following question:

Hello, all,

I'm using Test::More and Moose.

My minimal Moose class looks a little like this:

package Foo; use Moose; has 'first_name' => ( is => 'rw', isa => 'Str', required => 1); has 'last_name' => ( is => 'rw', isa => 'Str', required => 1); no Moose; 1;

My new.t file has the following:

use Foo; use Test::More tests => 2; ok( my $foo = Foo->new( first_name => 'Joe', last_name => 'Average', ) ); ok( my $incomplete = Foo->new( first_name => 'Joe', ) );

Now, I know the second test will fail; it's what I'm testing for. What I don't know is, how do I tell Test::More that failure is what I'm hoping for? I've tried wrapping it in an eval and testing $@, but it still reports as a failure.

Am I missing something glaringly obvious here?

Replies are listed 'Best First'.
Re: Deliberate failure in Test::More
by ikegami (Pope) on Jun 07, 2010 at 01:47 UTC
    If you wanna test whether an exception was thrown, try to catch one and report whether one was thrown or not.
    my $incomplete = eval { Foo->new( first_name => 'Joe', ) }; ok( !$incomplete );

    You could be more specific and check whether the right exception was thrown.

    You might also like Test::Exception

      Thanks. I was trying to do that, but somewhere I was spacing out on capturing it. I'll check out Test::Exception.
Re: Deliberate failure in Test::More
by herveus (Parson) on Jun 07, 2010 at 12:08 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-12-03 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (29 votes). Check out past polls.

    Notices?