使用Test :: Warn测试错误循环抛出

时间:2017-02-06 16:09:30

标签: perl testing

我有以下创建数据库的子例程:

sub create_db {
  my $self = shift;
  my $name = shift;
  $self->dbh->do("create database $name")
}

这个子程序由这个子程序调用,它在循环中生成几个数据库:

sub add_all_databases {
  my $self = shift;

  foreach my $year (@{$self->years}) {
    my $name = DB_NAME_PREFIX . $year;
    $self->create_db($name);
  }
}

如果数据库已存在,Perl会生成警告:

DBD::mysql::db do failed: Can't create database 'db_2014'; database exists

我想创建一个测试来检查数据库是否已存在时是否抛出警告。我尝试过这个测试:

 use Test::Warn

 warnings_like {$i->add_all_databases} qr{do failed}, 'throws error if database exists';

然而,它似乎不起作用。我从我的测试脚本得到这样的输出:

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)

如何正确编写将通过的测试?

1 个答案:

答案 0 :(得分:4)

{% extends '@GroupGroup/Group/groupe_mur_base.html.twig' %} {% block panel %} <div class="jumbotron list-content" style="display: block;"> <ul class="list-group"> <li href="#" class="list-group-item title"> Liste des Membres </li> {% for m in mem %} <li href="{{ path('your_path_to_member_details') }}" class="list-group-item text-left" id="listmembre" style="display: block;"> <div class="image"> <img class="img-thumbnail" src="{{ asset(m.profile_pic) }}"/> <span class="membername"> {{ m.prenom }} {{ m.nom }} </span> </div> <div class="break"></div> </li> {% endfor %} </ul> <div> <button id="btnajout" class="btn btn-primary"> Ajouter </button> </div> </div> {% endblock %} 的第二个参数应该是对包含与警告一样多的元素的数组的引用:

warnings_like

另外,创建具有不同数字后缀的多个数据库是一个主要的代码味道。通常情况下,拥有一年中具有列的单个数据库会更好。