5

I have read Test::Mojo but do not find how to use route name while application testing:

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

Where list_users and auth_login are:

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

I seems to me it will be very handy if *_ok will count given string same as redirect_to does. VOTEUP if this looks good for feature request to you too.

As work around this problem I try to use url_for without success:

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

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

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158

2 Answers2

1

I use Test::Mojo::Role as adviced by 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, $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

That is better to do reverse: try to get route name from location and compare it to expected result

simbabque
  • 53,749
  • 8
  • 73
  • 136
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 2
    You shouldn't overload private methods, they can and may be changed without notice even in a minor version. – Joel Berger Dec 03 '16 at 16:24
  • @JoelBerger; Yes, but in this case I should overload `get_ok`, `post_ok` etc. Is there workaround to do not overload private method? – Eugen Konkov Dec 03 '16 at 16:41
  • I'd probably make a new one called something like `request_ok` or `route_ok` and have it take a method as its first argument. Otherwise yes, you would do best to explicitly overload all the methods that you are changing. – Joel Berger Dec 03 '16 at 18:17
  • Also, building a controller just to call `url_for` could be replaced by talking to the app's router instance directly – Joel Berger Dec 03 '16 at 18:20
  • 1
    @JoelBerger Thanks, I have found that: `$t->app->routes->lookup( $url )->render` – Eugen Konkov Dec 03 '16 at 19:09
0

We may build_controller to call url_for:

$t->app->build_controller->url_for( 'list_users' )
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158