skip to Main Content

When I’m testing get route in Laravel I am getting error that token field is required.

$response = $this->get(route('resendsms', ['token' => $token]));

web route

Route::get('/auth/resendsms/{token}', [AuthController::class, 'resendsms'])->name('resendsms');

I seem not to have the right syntax

What am I doing wrong?

Thank you

Things I tried

$response = $this->get(route('resendsms', ['token' => $token]));
$response = $this->get(route('resendsms',$token));

2

Answers


  1. When testing a route that requires a parameter, you should pass the parameter as part of the route URL. However, based on your code and the error you’re encountering, it seems like the route definition in your web.php file is expecting the {token} parameter in the URL, but your test code is not providing it correctly.

    Here’s how you can correctly pass the token parameter when testing the route:
    // Assuming $token is the actual token you want to pass
    $response = $this->get(‘/auth/resendsms/’ . $token);

    If you prefer using the route helper, you can pass the parameters as the second argument:

    $response = $this->get(route(‘resendsms’, [‘token’ => $token]));

    Login or Signup to reply.
  2. Follow the example and check the parameters of the AuthController@resendsms method.

    Route::get('/auth/resendsms/{token}', function (string $token) {
        // ...
    })->name('resendsms');
     
    $url = route('resendsms', ['token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9', 'photos' => 'yes']);
    
    $response = $this->get($url);
    
    $response->assertStatus(200);
     
    // $url - /auth/resendsms/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9?photos=yes
    

    Follow the example and check the parameters of the AuthController@resendsms method.

    You can also use the

    $this->call('GET', '/auth/resendsms/{token}', ['token' => $token]);
    

    Also be sure to clear your cache

    php artisan route:clear
    

    I also recommend making parameters of the right type

    })->where(['token' => '[a-zA-Z0-9]+'])->name('resendsms')
    

    Laravel Routing

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search