我已阅读
Test::Mojo但在应用程序测试时没有找到如何使用路由名称:
$t->get_ok( 'list_users' )->status_is( 302 )->location_is( 'auth_login' );
list_users和auth_login是:
$r->get( '/login' )->to( 'auth#login' )->name( 'auth_login' ); $r->get( '/users' )->to( 'user#index' )->name( 'list_users' );
在我看来,如果* _ok将计算给定字符串与redirect_to相同,这将非常方便. VoteUP,如果这也适合您的功能请求.
$t->url_for( 'list_users' ); #Can't locate object method "url_for" via package "Test::Mojo"
解决方法
我使用
Test::Mojo::Role作为Sebastian Riedel的建议:
package Test::Mojo::Role::MyRole; use Role::Tiny; use Test::More; sub _build_ok { my( $self,$method,$url ) = ( shift,shift,shift ); if( my $route = $t->app->routes->lookup( 'list_users' ) ) { $url = $route->render; } local $Test::Builder::Level = $Test::Builder::Level + 1; return $self->_request_ok( $self->ua->build_tx( $method,$url,@_ ),$url ); } sub location_is { my( $t,$desc ) = @_; if( my $route = $t->app->routes->lookup( 'list_users' ) ) { $url = $route->render; } $desc //= "Location: $url"; local $Test::Builder::Level = $Test::Builder::Level + 1; return $t->success( is($t->tx->res->headers->location,$desc) ); } #myapp.t use Test::Mojo::WithRoles 'MyRole'; our $t = Test::Mojo::WithRoles->new( 'MyApp' ); $t->get_ok( 'list_users' )->status_is( 302 )->location_is( 'auth_login' );
UPD
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。