需要登录的测试控制器

时间:2019-11-27 14:22:58

标签: php laravel testing phpunit

我正在学习PHPUnit,我想测试需要登录的页面(例如更改密码,添加新文章等)。正常测试看起来像这样

$response = $this->get('/');

 $response->assertStatus(200);

但是,如果页面需要身份验证(适用于登录用户),它将无法正常工作。我该如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

使用Laravel运行测试时,您可以“充当”用户

$user = \App\User::find(1) //replace with creating or finding the user you want
$this->actingAs($user);