perl – 如何在Mojolicious中测试重定向?

前端之家收集整理的这篇文章主要介绍了perl – 如何在Mojolicious中测试重定向?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想测试一个带有表单的页面,该表单在提交时将重定向到提交项目的结果页面.

我的MojolicIoUs控制器包含:

sub submit_new {
    my $self = shift;

    my $new = $self->db->resultset('Item')->new( {
        title       => $self->param('title'),description => $self->param('description'),} );
    $new->insert;

    # show the newly submitted item
    my $id = $new->id;
    $self->redirect_to("/items/$id");
}

该控制器的测试脚本包含:

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

my $t = Test::Mojo->new('MyApp');

my $tx = $t->ua->build_form_tx('/items/new/submit' => $data);
$tx->req->method('POST');
$t->tx( $t->ua->start($tx) )
  ->status_is(302);

我的问题是它以302状态停止.如何继续重定向,以便我可以验证生成的项目页面

解决方法

从Mojo :: UserAgent设置匹配设置:

$t->ua->max_redirects(10)

此外,您不需要手动构建表单帖子:

$t->post_form_ok('/items/new/submit' => $data)->status_is(...);

参考:

> http://mojolicio.us/perldoc/Test/Mojo#ua
> http://mojolicio.us/perldoc/Mojo/UserAgent#max_redirects
> http://mojolicio.us/perldoc/Test/Mojo#post_form_ok

猜你在找的Perl相关文章