Reputation: 31
I am trying to do login test by phpunit in laravel. I have 5.5 so visit method is not supported. Here is what I am doing
public function testLoginPost(){
Session::start();
$response = $this->call('POST', 'login', [
'email' => '[email protected]',
'password' => '123456',
'_token' => csrf_token()
]);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('auth.login', $response->original->name());}
```
c:\wamp64\www\fundtheneedy\tests\Unit>phpunit Fundtheneedy
PHPUnit 6.5.8 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 321 ms, Memory: 14.00MB
There was 1 failure:
1) Tests\Unit\Fundtheneedy::testLoginPost
Failed asserting that 302 matches expected 200.
C:\wamp64\www\fundtheneedy\tests\Unit\Fundtheneedy.php:30
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
```
Upvotes: 2
Views: 2083
Reputation: 830
If you purely look at the response code it will always be 302 which is the response code for a URL Redirect as it will redirect regardless whether or not it fails.
You can instead look at whether or not there are errors in the session which is a bit of a workaround mentioned in this post here.
$response->assertSessionMissing('errors');
Or use the inverse
$response->assertSessionHasErrors();
Upvotes: 1
Reputation: 1840
Given you'll always be redirected, regardless of a successful or unsuccessful login, you'll have to assert the response you got.
To test a successful login, you would do:
$response->assertStatus(302);
$response->assertRedirect('http://your-website.com/user/dashboard');
To test an unsuccessful login, you would do:
$response->assertStatus(302);
$response->assertRedirect('http://your-website.com/auth/login');
That should be enough.
PS: Edit the urls to match whatever you have, obviously.
Upvotes: 0