0

this is my code laravel unit testing for login

public function testClientSucceslogin()
{
    $user = factory(User::class)->create([
        'password' => 'secret',
        'status' => User::STATUS_ACTIVE,
        'type' => User::TYPE_CLIENT
    ]);
    $clientProfile = ClientProfile::where('client_id', $user->client_id)->first();
    $client_alias = Client::where('id', $user->client_id)->first();

    $this->get(route('login', [$user->email, $user->password]))
        ->assertStatus(200)
        ->assertRedirect('dashboard', [$client_alias->alias, $clientProfile->id])
        ->assertTrue(true);


}

and I got this error

Response status code [200] is not a redirect status code. Failed asserting that false is true.

and i changed status code to 302 but i got this error

enter code heExpected status code 302 but received 200.

Failed asserting that false is true.

How I can fix this issue?

flower
  • 989
  • 4
  • 16
  • 34
  • `->assertResponseStatus($code);` if you are trying to assert the response status code given use that – iamcaleberic Jan 28 '18 at 09:27
  • Call to undefined method Illuminate\Http\Response::assertResponseStatus() @iamcaleberic – flower Jan 28 '18 at 09:29
  • What is unclear? You start by expecting 200 and a redirect. This doesn't make sense, since 200 means success, not redirect. Then you expect 302 and redirect. That makes sense, but the test fails because what you actually receive is 200, not 302. So, either the code is incorrect because it should redirect, but doesn't, or the test is incorrect, because it expects a redirect and a code 302 although the code is supposed to return a 200. – JB Nizet Jan 28 '18 at 09:32
  • @flower i think there might be something wrong with your code. – iamcaleberic Jan 28 '18 at 09:35
  • I suspect the page is doing a redirect through Javascript or an HTML meta tag. If that's the case, the redirect is handled by the browser, not through the protocol layer. – jessehouwing Jan 28 '18 at 13:09

1 Answers1

0

assertRedirect() first tests your status code. If you don't follow the rfc, you might just want to check if a Location header is present in the response.

In 5.8+, you can do it via assertLocation():

$this->get(route('login', [$user->email, $user->password]))
     ->assertStatus(200)
     ->assertLocation('your-location')
     ->assertTrue(true);

In 5.4, you have do use PHPUnit::assertEquals()

$response = $this->get(route('login', [$user->email, $user->password]));
$response->assertStatus(200)
     ->assertTrue(true);
PHPUnit::assertEquals(app('url')->to($uri), $response->headers->get('Location'));
shaedrich
  • 5,457
  • 3
  • 26
  • 42