2011-06-29 25 views
8

我不確定正確的方法來設置www機械化的腳本應用程序。我確實嘗試了至少一個可用的替代方案,但是我試圖在測試中傳遞配置信息,這樣我就可以使測試套件的日誌安靜了。如何使用Test :: WWW :: Mechanize :: PSGI測試Dancer應用程序?

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

我把這個從Dancer::Deployment documentation的普拉克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 

DBI錯誤在這裏是不相關的,但它們的錯誤輸出部分是我得到的。我找不到爲什麼它找不到/login/default.tt。我猜測這是問題在於,它不知道我的視圖文件夾位於views/login/default.tt的位置。即使在plackup上運行,該視圖也能在瀏覽器中正常工作。我很難過。

回答

3

此作品在我的符號鏈接t/viewsviews我目前認爲這可能是一個錯誤的結果的情況下,所以我申請一個here,創造this test case repository

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

希望有人能爲我提供一個比我能夠解決的問題更好的答案。

+0

接受我自己的迴應,因爲現在它的工作原理,希望有一天有人能夠提供更好的東西。 – xenoterracide

相關問題