Search code examples
phplaravelunit-testingphpunittdd

Laravel - test. How to fix the Error Symfony\Component\HttpKernel\Exception\NotFoundHttpException


I can't run this test method, what I want to do is that only the administrator user can enter the panel, if the user is only a user, it must receive the status 302 but it receives 404,

If I run php artisan route:list I can see the route

Here is my test method:

/** @test */
   public function usuario_no_administrador_no_puede_entrar_al_panel()
   {

           $role = factory(Role::class)->create(['name' => 'usuario']);
           $user = factory(User::class)->create();
           $user->roles()->sync($role);

           $this->actingAs($user);

           $this->withoutExceptionHandling();
           $this->get('/panel')->assertStatus(302);

   }

Here is my web.php file:

Route::group(['middleware' => 'role:usuario', 'middleware' => 'role:administrador'], function() {
  Route::get('/panel', 'PanelController@index')->name('panel.index');
});

And here is the error:

Image of error when running php artisan test


Solution

  • You have two middleware keys in your route group definition. You’ll need to combine them, as otherwise the last one will override any previously-set ones:

    Route::group(['middleware' => ['role:usuario', 'role:administrador']], function() {
        Route::get('/panel', 'PanelController@index')->name('panel.index');
    });