我不确定为 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的Dancer::Deployment 文档中获取了这个。但是,我从测试中收到 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">'/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
DBI 错误在这里不相关,但它们是我得到的错误输出的一部分。我不知道为什么它找不到/login/default.tt
。我猜它的问题是它不知道我的视图文件夹在哪里,因为有问题的模板在views/login/default.tt
. 即使在plackup
. 我难住了。