当我运行失败的测试时,我得到了一个带有大量标记的巨大输出,隐藏了错误。
示例:
$ 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't call method "name" 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-Markup,以便我可以立即看到错误信息?
答案 0 :(得分:3)
这里有两件事情。
具有完整页面源的大型调试输出是因为content_like
method from Test::Mojo找不到匹配项,并且它告诉您它正在查找的字符串。这是一种方便的方法,但如果页面很大,那就是很多文本。这可能会告诉您测试失败,因为内容错误。但在这个具体案例中却没有。
真正的问题是测试失败,因为您有语法错误。你可以从最初的测试中看到它。
$t->get_ok('/info/penx2') ->status_is(200)
此测试也失败了。 (对于习惯于测试:: WWW :: Mechanize的人来说有点混乱,因为get_ok
还会检查响应是否为200 OK
。)
# Failed test '200 OK' # at t/2410-topinfo.t line 12. # got: '500' # expected: '200'
实际的错误消息应该在没有其他地方的所有HTML标记的情况下存在,因为当它正在执行get_ok
时它会遇到错误,该错误应该转到应用程序日志。在单元测试中,可能是STDERR。
我不知道你是否没有加入它,或者它是否被省略了。我也相信日志应该在那里。
回到HTML和实际问题,它输出的原因是因为Test :: Mojo的content_like
(及其大多数其他方法)使用了Test :: More。 It just dispatches到like
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
调用输出。