lundi 18 janvier 2021

Failed asserting that 200 is identical to 302. in laravel feature testing

I am running test in laravel . But it is failing test. I am not getting the reason may because of middleware but not sure.

Here is test function

public function test_example()
    {
        $staff = factory(Staff::class)->create();
        $response = $this->actingAs($staff) 
        ->withSession(['foo' => 'bar'])
            ->get('/products');

        $response->assertStatus(200);
    }
}

Here is the output

   PHPUnit\TextUI\Command::main()


  Tests:  1 failed, 2 passed
  Time:   0.98s

PS C:\projects\coldxlogistics> php artisan test
Warning: TTY mode is not supported on Windows platform.

   PASS  Tests\Feature\DashboardTest
  ✓ dashboard loads fine
  ✓ user cannot see dashboard without login

   FAIL  Tests\Feature\ProductTest
  ⨯ example

  ---

  • Tests\Feature\ProductTest > example
  Expected status code 200 but received 302.
  Failed asserting that 200 is identical to 302.

  at C:\projects\coldxlogistics\tests\Feature\ProductTest.php:24
     20▕         $response = $this->actingAs($staff)
     21▕         ->withSession(['foo' => 'bar'])
     22▕             ->get('/products');
     23▕
  ➜  24▕         $response->assertStatus(200);
     25▕     }
     26▕ }
     27▕

  1   C:\projects\coldxlogistics\vendor\phpunit\phpunit\phpunit:61
      PHPUnit\TextUI\Command::main()


  Tests:  1 failed, 2 passed
  Time:   1.00s

There are multiple middlwares working on routes. for Example

auth,login_auth,two_factor_auth

and what is the purpose of withSession() how to use it? i see the documentation but can;t understand what is foo, bar



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire