2017-07-07 31 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