如何进行Perl Dancer测试

时间:2015-06-20 20:04:08

标签: perl testing dancer

阅读Dancer ::测试文档使得测试看起来很简单,但我遗漏了一些东西。如果我有以下Dancer应用程序(WebApp.pm):

package WebApp;
use Dancer;

# declare routes/actions
get '/' => sub {
    "Hello World";
};

dance;

然后是以下测试文件001_base.t

use strict;
use warnings;
use Test::More tests => 1;

use WebApp;
use Dancer::Test;

response_status_is [GET => '/'], 200, "GET / is found";

然后当我运行测试:perl 001_base.t时,输出是舞者脚本启动:

Dancer 1.3132 server 7679 listening on http://0.0.0.0:3000
== Entering the development dance floor ...

然后等待。 (这与我在WebApp.pm中运行代码的情况相同)。我在这里错过了什么?我想我没有正确运行测试。

1 个答案:

答案 0 :(得分:0)

您应该从WebApp.pm中删除dancer()。这是正确的内容:

package WebApp;
use Dancer;

# declare routes/actions
get '/' => sub {
    "Hello World";
};

1;

然后你测试将通过。

创建舞者应用程序的常用方法是在一个或多个.pm文件中声明所有路由,并且通常使用内容名称为app.psgi的文件:

#!/usr/bin/env perl
use Dancer;
use WebApp;
dance;

然后,要启动您的Web应用程序,您应该运行perl -Ilib app.psgi