8

wwwmechanizeのスクリプトアプリを設定する正しい方法がわかりません。動作する代替案を少なくとも1つ試しましたが、テストスイートを使用してロギングをより静かにすることができるように、テストで構成を渡そうとしています。

#!/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;

これは、PlackPSGIの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">&#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

DBIエラーはここでは関係ありませんが、私が取得するエラー出力の一部です。なぜ見つからないのかわかりません/login/default.tt。問題のテンプレートがにあるので、ビューフォルダがどこにあるのかわからないことが問題だと思いますviews/login/default.tt。このビューは、で実行している場合でもブラウザで正常に機能しplackupます。私は困惑しています。

4

1 に答える 1

3

これは、私がシンボリックリンクt/viewsしている状況下で機能します。views現在、これはおそらくバグの結果であると考えているため、ここにファイルし、このテスト ケース リポジトリを作成しました。

#!/usr/bin/perl
use strict;
use warnings;
BEGIN {
    use Test::More;
    use namespace::clean qw( pass );
}
use FindBin;
use Cwd qw( realpath );
use Dancer qw( :syntax );
#use MyApp;
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';
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance( $request );
    }
);

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

done_testing;

ロガーを設定してログインしenvironments/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.

うまくいけば、誰かが私が打ち破ることができたよりも良い答えを提供してくれることを願っています.

于 2011-06-30T17:26:24.173 に答える