Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BishoyWagih's avatar

Expected status code 200 but received 302.

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());
}
0 likes
5 replies
rin4ik's avatar

try to change this

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

to this

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

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

ITACDEVEL's avatar

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.

AungKhantZaw's avatar

it because you use $response->assertStatus(200) this assertStatus is only work on $this

Please or to participate in this conversation.