testing - 如何在单元测试中摆脱错误 422 laravel?

标签 testing laravel-5

所以我正在为一个 laravel 5.7 网络应用程序编写单元测试,当我测试登录时它给我错误 422(我知道它与无效数据有关,我只是不知道如何修复它)

public function testRegularUserLogin_CreatedRegularUse_ReturnsStoreView()
{
    $regularUser = factory( User::class)->create();

    $response = $this->json('POST','/login',
        [
            'email' => $regularUser->email,
            'password' => $regularUser->password,
            '_token' => csrf_token()
        ]);


    $response->assertStatus(200);


}

我试过在标题上使用 csrf token

This is the output that test gives me

最佳答案

你应该只是模拟身份验证: 做这样的事情

public function getFakeClient()
{
    $client = factory(App\User::class)->create();
    $this->be($client);

    Auth::shouldReceive('user')->andReturn($this->client);
    Auth::shouldReceive('check')->andReturn(true);

    return $this->client;
}

然后

$user = $this->getFakeClient();
$user->shouldReceive('posts')->once()->andReturn(array('posts'));

由 Taylor Otwell 本人推荐 here .

关于testing - 如何在单元测试中摆脱错误 422 laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917920/

相关文章:

animation - 在哪里下载很酷的动画进行测试?

php - 模拟包装器时间函数

angular - 编写多个测试用例时,ActivatedRoute 参数在单元测试中不起作用

php - 如何在 PHP Laravel 5 中配置 Amazon SES SMTP?

php - 如何在 Laravel 5 的所有 View 中共享 Auth::user?

php - Laravel 后 Controller 不工作(Symfony\Component...)

maven-2 - 在 Maven 中共享测试代码

angular - fixture.debugElement.componentInstance 和 fixture.nativeElement 有什么区别?

php - Laravel 文件上传 : Accessing the file from server

php - 在 Laravel 中将值从 url 传递到 Controller