Expected status code 200 but received 302.

Published 1 month ago by BishoyWagih

i'm trying to perform a test to check if 1 - the user is authenticated 2 - has permission to view the requested page

every time i run the test i get this error

Expected status code 200 but received 302.

Here is my TestMethod

 /** @test */

public function it_can_see_all_categories()
{
    $user = $user = factory(User::class)->create();
    $this->actingAs($user);

    $permission = Permission::create(['group' => 'categories' , 'name' => 'view categories' , 'label' => 'view categories']);

    $role = Role::find($user->role_id);

    $role->givePermissionTo($permission);

    $response = $this->get('/categories');

    $response->assertStatus(200 , $response->getStatusCode());
}
rin4ik
rin4ik
1 month ago (199,110 XP)

try to change this

  $user = $user = factory(User::class)->create();

to this

  $user = factory(User::class)->create();
BishoyWagih

this is typing mistake from me here, not in the code

ITACDEVEL

Does your route for '/categories' use any middleware such as auth? That would be returning a 302 when it redirects you to the auth/login page whenever it gets hit.

BishoyWagih

yes i have a middleware auth, also i have authorization on request with permission view categories, please see this alse,,

https://laracasts.com/discuss/channels/testing/php-unit-need-help

Please sign in or create an account to participate in this conversation.