Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 (Patriarch) 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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-12-10 02:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found