6

我想测试一个带有表单的页面,该表单在提交时将重定向到提交项目的结果页面。

我的 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状态而停止。如何进行重定向,以便验证生成的项目页面?

4

2 回答 2

9

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

$t->ua->max_redirects(10)

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

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


参考:

于 2012-09-08T06:18:30.937 回答
1

您还可以(并且可能应该)测试成功登录重定向到的登录页面的内容:

my $tm = '' ; # the test message for each test
my $t = Test::Mojo->new('Qto');
$t->ua->max_redirects(10);
my $app = 'foobar';
my $url = '/' . $db_name . '/login' ;

 $tm = "a successfull result redirects to the home page";
 my $tx = $t->ua->post( $url => 'form'  => {'email' =>'test.user@gmail.com', 'pass' => 'secret'});
 ok ( $tx->result->dom->all_text =~ "$app home" , $tm );

如何使用 mojo dom 获取内容的文档

mojo dom 选择器语法文档链接

带有登录测试的示例测试脚本

于 2019-07-09T10:50:48.950 回答