laravel - Laravel登录测试在phpunit中失败
问题描述
我正在尝试在 laravel 中通过 phpunit 进行登录测试。我有 5.5 所以不支持访问方法。这是我正在做的
public function testLoginPost(){
Session::start();
$response = $this->call('POST', 'login', [
'email' => 'sokhter@yahoo.com',
'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.
```
解决方案
如果您只看响应代码,它将始终是 302,这是 URL 重定向的响应代码,因为无论它是否失败,它都会重定向。
相反,您可以查看会话中是否存在错误,这是本文中提到的一种解决方法。
$response->assertSessionMissing('errors');
或者使用逆
$response->assertSessionHasErrors();