How to use a route name when testing a mosaic application?

I read Test :: Mojo , but did not find how to use the route name while testing the application:

$t->get_ok( 'list_users' )->status_is( 302 )->location_is( 'auth_login' );

Where list_usersand auth_login:

$r->get( '/login' )->to( 'auth#login' )->name( 'auth_login' );
$r->get( '/users' )->to( 'user#index' )->name( 'list_users' );

It seems to me that it will be very convenient if *_okit considers this line the same as redirect_to. VOTEUP if it is well suited to request a function.

How to get around this problem, I try to use url_forwithout success:

$t->url_for( 'list_users' );
#Can't locate object method "url_for" via package "Test::Mojo"

How can I get a route route by its name from a test script?

+4
source share
2 answers

Test:: Mojo:: Role, :

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, $url, $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, $url, $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

:

0

build_controller url_for:

$t->app->build_controller->url_for( 'list_users' )
0

Source: https://habr.com/ru/post/1662729/


All Articles