Bar*_*hul 4 authentication phpunit laravel-5 homestead
因此,我正在尝试在 Homestead 上运行的 Laravel 5.8 项目上测试注册和登录功能。
我的问题是我无法通过 assertAuthenticated() 和 assertAuthenticatedAs() 函数进行测试(用于登录和注册)。
我使用创建了登录功能php artisan make:auth并且没有做太多更改,只是创建了一个“用户名”字段来代替电子邮件。
当我测试诸如assertStatus(), 之类的$this->get(url)东西时,一切正常,但是当我添加该行时$this->assertAuthenticatedAs($user),测试崩溃了。
这是我实际的传递函数:
public function test_login_valid_user()
{
$user = factory(User::class)->create();
$response = $this->post('/login', [
'username' => $user->username,
'password' => 'secret'
]);
$response->assertStatus(302);
}
Run Code Online (Sandbox Code Playgroud)
如果我$this->assertAuthenticatedAs($user)在最后添加该行,则会出现以下错误:
There was 1 failure:
1) Tests\Feature\Auth\LoginTest::test_login_valid_user
The current user is not authenticated.
Failed asserting that null is not null.
/home/vagrant/code/my_project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php:89
/home/vagrant/code/my_project/tests/Feature/Auth/LoginTest.php:39
Run Code Online (Sandbox Code Playgroud)
在我的注册测试中也发生了同样的情况,在用户注册后,当我尝试检查时$this->assertAuthenticated()出现相同的错误。
所以,我想到了与 Vagrant/Homestead 相关的会话问题,但我刚刚开始使用它们并且找不到任何关于它的提示。我对 PHPUnit 和一般测试非常陌生,我才刚刚开始了解它是如何工作的。
问题与缓存有关。
首先phpunit.xml必须读取文件,因为您需要:<server name="APP_ENV" value="testing"/>
在您的测试使用命令之前
php artisan config:clear
Run Code Online (Sandbox Code Playgroud)
在那之后你dump(config('app.env'));会testing(不是local)。
然后一切正常。
| 归档时间: |
|
| 查看次数: |
1587 次 |
| 最近记录: |