Perl 如何使用test::WWW::Mechanize::PSGI测试Dancer应用程序?

Perl 如何使用test::WWW::Mechanize::PSGI测试Dancer应用程序?,perl,testing,www-mechanize,dancer,psgi,Perl,Testing,Www Mechanize,Dancer,Psgi,我不确定如何为www mechanize设置脚本应用程序。我确实尝试了至少一个可行的替代方案,但是我正在尝试通过测试的配置,以便使用测试套件使日志记录更安静 #!/usr/bin/perl use strict; use warnings; use Dancer qw(:syntax); use MyApp; use Test::More; use Test::WWW::Mechanize::PSGI; set apphandler => 'PSGI'; set log => 'wa

我不确定如何为www mechanize设置脚本应用程序。我确实尝试了至少一个可行的替代方案,但是我正在尝试通过测试的配置,以便使用测试套件使日志记录更安静

#!/usr/bin/perl
use strict;
use warnings;
use Dancer qw(:syntax);
use MyApp;
use Test::More;
use Test::WWW::Mechanize::PSGI;
set apphandler => 'PSGI';
set log => 'warning';
set logger => 'note';

my $mech = Test::WWW::Mechanize::PSGI->new(
    app => dance, # app => do('bin/app.pl'), #
);

$mech->get_ok('/login') or diag $mech->content;
done_testing;
在脚本上运行
do
似乎允许测试运行,但日志变量设置不正确,同时似乎有更好的方法来实现这一点

更新

我想我可能正在接近一个解决方案

#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use Cwd qw( realpath );
use Dancer qw(:syntax);
use MyApp;
use Test::More;
use Test::WWW::Mechanize::PSGI;
set apphandler => 'PSGI';

my $appdir = realpath( "$FindBin::Bin/.." );
my $mech = Test::WWW::Mechanize::PSGI->new(
    app => sub {
        my $env = shift;
        setting(
            appname => 'MyApp',
            appdir => $appdir,
        );
        load_app 'MyApp';
        config->{environment} = 'test'; # setting test env specific in test.yml detected ok
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance( $request );
    }
);

$mech->get_ok('/login') or diag $mech->content;


done_testing;
这是我从Plack PSGI上拿的。然而,我从测试中得到了500个错误

t/001-login.t .. Subroutine main::pass redefined at t/001-login.t line 8
Prototype mismatch: sub main::pass: none vs (;$) at t/001-login.t line 8
Use of uninitialized value $_[0] in join or string at    /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/i686-linux/File/Spec/Unix.pm line 86.

# [2462] debug @0.004442> [hit #1]Adding mysql_enable_utf8 to DBI connection params to   enable UTF-8 support in /home/ccushing/perl5/perlbrew/perls/perl- 5.14.1/lib/site_perl/5.14.1/Dancer/Plugin/Database.pm l. 148
# [2462] debug @0.117566> [hit #1]Adding mysql_enable_utf8 to DBI connection params to enable UTF-8 support in /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Plugin/Database.pm l. 148
# [2462] error @0.148703> [hit #1]request to /login crashed: '/login/default.tt' doesn't exist or not a regular file at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer.pm line 161 in /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Handler.pm l. 84
# <h2>runtime error</h2><pre class="error">&#39;/login/default.tt&#39; doesn&#39;t exist or not a regular file at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer.pm line 161
t/001 login.t。。子程序main::pass在t/001 login.t第8行重新定义
原型不匹配:sub-main::pass:none vs(;$)在t/001 login.t第8行
在/home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/i686-linux/File/Spec/Unix.pm第86行的连接或字符串中使用未初始化的值$U0。
#[2462]debug@0.004442>[hit#1]将mysql_enable_utf8添加到DBI连接参数,以在/home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Plugin/Database.pm l中启用UTF-8支持。148
#[2462]debug@0.117566>[hit#1]将mysql_enable_utf8添加到DBI连接参数,以在/home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Plugin/Database.pm l中启用UTF-8支持。148
#[2462]error@0.148703>[hit#1]请求/login崩溃:“/login/default.tt”在/home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer.pm第161行/home/ccushing/perl5/perl-5.14.1/lib/site#/Handler.pm l。84
#运行时错误'/login/default.tt';不';t在/home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer.pm第161行中是否存在常规文件

DBI错误在这里不相关,但它们是我得到的错误输出的一部分。我不明白为什么它找不到
/login/default.tt
。我猜问题在于它不知道我的视图文件夹在哪里,因为所讨论的模板位于
views/login/default.tt
。即使在
plackup
上运行,此视图在浏览器中也能正常工作。我被难住了。

这在我将
t/views
符号链接到
views
的情况下有效,我目前认为这可能是一个bug的结果,所以我提交了一个,并创建了一个

我设置记录器并登录
环境/test.yml

我仍然会遇到这些错误,我希望看到它们得到修复,但不确定是什么导致了这些错误

  Use of uninitialized value $_[0] in join or string at  /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/i686-linux/File/Spec/Unix.pm line 86.
Use of uninitialized value $path in -e at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/FileUtils.pm line 46.
Use of uninitialized value in index at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Renderer.pm line 160.

希望有人能给我一个比我所能提供的更好的答案。

接受我自己的回答,因为现在它起作用了,希望有一天有人能提供更好的答案。
  Use of uninitialized value $_[0] in join or string at  /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/i686-linux/File/Spec/Unix.pm line 86.
Use of uninitialized value $path in -e at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/FileUtils.pm line 46.
Use of uninitialized value in index at /home/ccushing/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Dancer/Renderer.pm line 160.