I have the following subroutine that creates a database:
sub create_db {
my $self = shift;
my $name = shift;
$self->dbh->do("create database $name")
}
This subroutine is called by this one which generates several databases in a loop:
sub add_all_databases {
my $self = shift;
foreach my $year (@{$self->years}) {
my $name = DB_NAME_PREFIX . $year;
$self->create_db($name);
}
}
Perl generates a warning if a database already exists:
DBD::mysql::db do failed: Can't create database 'db_2014'; database exists
I want to create a test to check that that the warning is thrown when a database already exists. I attempted that with this test:
use Test::Warn
warnings_like {$i->add_all_databases} qr{do failed}, 'throws error if database exists';
However, it does not seem to work. I get output like this from my test script:
not ok 17 - throws error if database exists
# Failed test 'throws error if database exists'
# at ./importer.t line 69.
# found warning: DBD::mysql::db do failed: Can't create database 'db_2000'; database exists at /home/steve/perl/perl-lib/DB.pm line 46, <DATA> line 1.
# found warning: DBD::mysql::db do failed: Can't create database 'db_2001'; database exists at /home/steve/perl/perl-lib/DB.pm line 46, <DATA> line 1.
# found warning: DBD::mysql::db do failed: Can't create database 'db_2002'; database exists at /home/steve/perl/perl-lib/DB.pm line 46, <DATA> line 1.
# expected to find warning: (?^:do failed)
How do I properly write a test that will pass?
Aucun commentaire:
Enregistrer un commentaire