加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:18:47 所属栏目:大数据 来源:网络整理
导读:我想测试一个带有表单的页面,该表单在提交时将重定向到提交项目的结果页面. 我的Mojolicious控制器包含: sub submit_new { my $self = shift; my $new = $self-db-resultset('Item')-new( { title = $self-param('title'),description = $self-param('descr
我想测试一个带有表单的页面,该表单在提交时将重定向到提交项目的结果页面.

我的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读