如何 运行 Perl Dancer 测试

How to run a Perl Dancer Test

通读 Dancer::Test 文档使进行测试看起来很简单,但我遗漏了一些东西。如果我有以下 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 中的代码一样)。我在这里错过了什么?我猜我 运行 测试不正确。

您应该从 WebApp.pm 中删除 dancer()。正确内容如下:

package WebApp;
use Dancer;

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

1;

那你测试就通过了。

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

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

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