2017-07-07 27 views
1

當我運行測試失敗時,我得到一個巨大的輸出,並且有很多隱藏錯誤的標記。在測試中刪除標記

實施例:

$ perl script/my_prove.pl t/2410-topinfo.t 
t/2410-topinfo.t .. 1/? 
# Failed test '200 OK' 
# at t/2410-topinfo.t line 12. 
#   got: '500' 
#  expected: '200' 

# Failed test 'similar match for selector "h1"' 
# at t/2410-topinfo.t line 12. 
#     '' 
#  doesn't match '(?^:Flatinfo\ Business\-Apartment\ Hietzing)' 

# Failed test 'content is similar' 
# at t/2410-topinfo.t line 12. 
#     '<!DOCTYPE html> 
# <html> 
# <head> 
#  <title>Server error (development mode)</title> 
#  <meta http-equiv="Pragma" content="no-cache"> 
#  <meta http-equiv="Expires" content="-1"> 
#  <script src="/mojo/jquery/jquery.js"></script> 
#  <script src="/mojo/prettify/run_prettify.js"></script> 
#  <link href="/mojo/prettify/prettify-mojo-dark.css" rel="stylesheet"> 
#  <style> 
#  a img { border: 0 } 
#  body { 
# 
# ........... lots of lines removed here ........... 
# 
#  <div id="wrapperlicious"> 
#   <div id="nothing" class="box spaced"></div> 
#   <div id="showcase" class="box code spaced"> 
#   <pre id="error">Can&#39;t call method &quot;name&quot; on an undefined value at template extern/topinfo/show.html.ep line 2. 
# </pre> 
# 
# .... lots of lines follow here ............ 

的誤差似乎是一個單行:

Can't call method "name" on an undefined value at template extern/topinfo/show.html.ep line 2 

的測試腳本產生此輸出是:

use Mojo::Base -strict; 

use Test::More; 
use Test::Mojo; 

use FindBin; 
require "$FindBin::Bin/../script/ba_db"; 
my $t = Test::Mojo->new('BaDb'); 
$t->ua->max_redirects(1); 

$t->get_ok('/info/penx2') 
    ->status_is(200) 
    ->text_like('h1' => qr/\QFlatinfo Business-Apartment Hietzing\E/) 
    ->content_like(qr/\QSelected language: German\E/) 
    # ... 
; 
done_testing(); 

有一種方法來告訴Mojolicious迴應沒有所有這些HTML標記,以便我ca n立即看到錯誤消息?

回答

3

有兩件事在這裏玩。

整個頁面源代碼的大型調試輸出是因爲content_like method from Test::Mojo找不到匹配項,並且它告訴你它正在查找哪個字符串。這是一種方便的方法,但如果頁面很大,則會有很多文字。這可能會告訴您測試失敗,因爲內容錯誤。但在這個特定的情況下,它沒有。

真正的問題是測試失敗,因爲您有語法錯誤。您可以從第一次測試中看到這一點。

$t->get_ok('/info/penx2') 
    ->status_is(200) 

該測試也失敗了。 (對於習慣於測試:: WWW ::機械化的人有點困惑,因爲get_ok也會檢查響應是否爲200 OK)。

# Failed test '200 OK' 
# at t/2410-topinfo.t line 12. 
#   got: '500' 
#  expected: '200' 

實際的錯誤信息應該是不存在所有的HTML標記其他地方,因爲它是做get_ok同時它會遇到錯誤,它應該去的應用程序日誌。在單元測試中,這可能是STDERR。

我不知道你是否沒有收錄它,或者它是否被省略。日誌應該在那裏我相信。


再回到HTML和實​​際問題,其輸出的原因是因爲測試::魔的content_like(以及大多數其他的它的方法)使用測試::引擎蓋下更多。 It just dispatcheslike from Test::More並傳遞頁面內容。這反過來將始終顯示它匹配的完整字符串。

在最近的Test :: More版本中,它已經在引擎蓋下使用了Test2。輸出完整字符串的相關部分是here

不幸的是,你可以做的不多。我會專注於找出爲什麼在單元測試期間它沒有顯示正確的日誌(可能是因爲您沒有運行prove-v),並且可能找到一種方法來使錯誤出現在顏色中,這會使它更容易閱讀。有a color logger for the Dancer2 framework(我維護),但 我找不到一個爲Mojo Mojo沒有一個。

現在有Mojo::Log::Colored,它可以根據日誌級別爲單獨的日誌行添加顏色。

use Mojo::Log::Colored; 

# Log to STDERR 
$app->log(
    Mojo::Log::Colored->new(

     # optionally set the colors 
     colors => { 
      debug => "bold bright_white", 
      info => "bold bright_blue", 
      warn => "bold green", 
      error => "bold yellow", 
      fatal => "bold yellow on_red", 
     } 
    ) 
); 

這會給你一個漂亮的彩色輸出到控制檯。這是一個示例腳本。

$ MOJO_LOG_LEVEL=debug perl -Mojo -MMojo::Log::Colored \ 
    -e 'a(
     "/" => sub { 
      app->log->$_("hello world") for qw/debug info warn error fatal/; 
      shift->render(text=>"ok"); 
     })->log(Mojo::Log::Colored->new)->start' \ 
    daemon 

而輸出如果調用$ curl localhost:3000

console output