http://www.perlmonks.org?node_id=843383

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