I am a bit new to the Devel::Cover module, but have found it very useful in making sure I am not missing tests.
A problem I am running into is understanding the report from Devel::Cover. I've looked at the documentation, but can't figure out what I need to test to get 100% coverage.
Here is the output from the cover report:
line err stmt bran cond sub pod time code
...
36 sub connect_database {
37 3 3 1 1126 my $self = shift;
38 3 100 24 if ( !$self->{dsn} ) {
39 1 7 croak 'dsn not supplied - cannot connect';
40 }
41 *** 2 33 21 $self->{dbh} = DBI->connect( $self->{dsn}, q{}, q{} )
42 || croak "$DBI::errstr";
43 1 11 return $self;
44 }
...
line err % l !l&&r !l&&!r expr
----- --- ------ ------ ------ ------ ----
41 *** 33 1 0 0 'DBI'->connect($$self{'dsn'}, '', '') || croak("$DBI::errstr")
And here is and example of my code that tests this specific line:
my $database = MyModule::Database->new( { dsn => 'Invalid DSN' });
throws_ok( sub { $database->connect_database() },
qr/Can't connect to data source/,
'Test connection exception (invalid dsn)' );
This test passes - the connect does throw an error and fulfills my "throws_ok" test.
I do have some tests that test for a successful connection, which is why I think I have 33% coverage, but if I'm reading it correctly, cover thinks I am not testing the "|| croak" part of the statement. I thought I was, with the "throws_ok" test, but obviously I am missing something.
Does anyone have advice on how I can test my DBI-connect line successfully?
Thanks!