我想测试一个带有表单的页面,该表单在提交时将重定向到提交项目的结果页面.
我的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);
解决方法
从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