身份验证测试失败,使用 Laravel、phpunit 和 Homestead
Failing authentication test, using Laravel, phpunit and Homestead
因此,我正在尝试在 Laravel 5.8 项目 运行 Homestead 上测试注册和登录功能。
我的问题是我无法让测试(用于登录和注册)通过 assertAuthenticated() 和 assertAuthenticatedAs() 函数。
我使用 php artisan make:auth
创建了登录功能并且没有做太多更改,只是创建了一个 "username" 字段来代替电子邮件。
当我测试 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);
}
如果我在末尾添加行 $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
我的注册测试也发生了同样的情况,在用户注册后,当我尝试检查时 $this->assertAuthenticated()
我得到了同样的错误。
所以,我想到了与Vagrant/Homestead相关的session问题,但我才刚刚开始使用它们,找不到任何提示。而且我对 PHPUnit 和一般测试还很陌生,我才刚刚开始了解它是如何工作的。
我也遇到过同样的问题。对于单元测试,应该禁用 CSRF 令牌验证,但前提是您 运行 在 APP_ENV=testing 下。我虽然 phpunit.xml 覆盖了我的 "local" 配置,所以它被设置为 "testing"。不是,因为 PhpStorm 没有读取这个文件。
如果您正在使用 PHPStorm,请不要忘记检查默认配置文件的路径 - phpunit.xml。 (设置 -> 语言和框架 -> PHP -> 测试框架)
问题与缓存有关。
首先必须读取文件 phpunit.xml
,因为您需要:<server name="APP_ENV" value="testing"/>
在测试之前使用命令
php artisan config:clear
之后你的 dump(config('app.env'));
将是 testing
(而不是 local
)。
那么一切正常。
因此,我正在尝试在 Laravel 5.8 项目 运行 Homestead 上测试注册和登录功能。
我的问题是我无法让测试(用于登录和注册)通过 assertAuthenticated() 和 assertAuthenticatedAs() 函数。
我使用 php artisan make:auth
创建了登录功能并且没有做太多更改,只是创建了一个 "username" 字段来代替电子邮件。
当我测试 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);
}
如果我在末尾添加行 $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
我的注册测试也发生了同样的情况,在用户注册后,当我尝试检查时 $this->assertAuthenticated()
我得到了同样的错误。
所以,我想到了与Vagrant/Homestead相关的session问题,但我才刚刚开始使用它们,找不到任何提示。而且我对 PHPUnit 和一般测试还很陌生,我才刚刚开始了解它是如何工作的。
我也遇到过同样的问题。对于单元测试,应该禁用 CSRF 令牌验证,但前提是您 运行 在 APP_ENV=testing 下。我虽然 phpunit.xml 覆盖了我的 "local" 配置,所以它被设置为 "testing"。不是,因为 PhpStorm 没有读取这个文件。
如果您正在使用 PHPStorm,请不要忘记检查默认配置文件的路径 - phpunit.xml。 (设置 -> 语言和框架 -> PHP -> 测试框架)
问题与缓存有关。
首先必须读取文件 phpunit.xml
,因为您需要:<server name="APP_ENV" value="testing"/>
在测试之前使用命令
php artisan config:clear
之后你的 dump(config('app.env'));
将是 testing
(而不是 local
)。
那么一切正常。