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
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]));
Follow the example and check the parameters of the
AuthController@resendsms
method.Follow the example and check the parameters of the AuthController@resendsms method.
You can also use the
Also be sure to clear your cache
I also recommend making parameters of the right type
Laravel Routing