Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
断言Json验证错误不起作用_Json_Laravel_Validation_Phpunit - Fatal编程技术网

断言Json验证错误不起作用

断言Json验证错误不起作用,json,laravel,validation,phpunit,Json,Laravel,Validation,Phpunit,我不是专家,我找不到关于我的错误的任何东西。我希望有人能帮助我理解我的错误 我试图在Laravel中测试POST请求所需属性的验证 测试 我正在使用assertJsonValidationErrors方法验证来自控制器的JSON错误 /** @test */ public function name_email_password_required_in_creation() { $response = $this->post('/users', [ 'contact

我不是专家,我找不到关于我的错误的任何东西。我希望有人能帮助我理解我的错误

我试图在Laravel中测试POST请求所需属性的验证

测试

我正在使用
assertJsonValidationErrors
方法验证来自控制器的JSON错误

/** @test */
public function name_email_password_required_in_creation()
{
    $response = $this->post('/users', [
        'contact' => '545678987'
    ]);
    $response->assertJsonValidationErrors(['name','password','email']);
}
控制器

$validator = Validator::make($request->all(),[
        'name' => 'required',
        'email' => 'required|unique:users,email',
        'password' => 'required',
    ]);

    if ($validator->fails()) {
        return response()->json($validator->errors(), 400);
    }
错误:

1)测试\Feature\UsersTest::name\u email\u password\u必需\u在创建中 在键“name”的响应中找不到验证错误。

响应没有JSON验证错误。 断言数组具有键“name”失败。

我申请了考试a

dd($response->getContent())要查看正在发生的情况以及响应,请执行以下操作:

“{”name:[“name字段是必需的。”],“email:[“email字段是必需的。”],“password:[“password字段是必需的。”]}”

这似乎是一个字符串,但我不知道是否是它导致了错误,因为JSON包含关键字段错误

我知道怎么解决它。提前感谢。

而不是:

$response = $this->post('/users', [
    'contact' => '545678987'
]);
使用标题接受json,如下所示:

$response = $this->json('POST', '/users', ['contact' => '545678987'],
    ['Accept' => 'application/json']
);
对于验证错误,响应代码为422,因此使用:

if ($validator->fails()) {
    return response()->json($validator->errors(), 422);
}
与此相反:

$response = $this->post('/users', [
    'contact' => '545678987'
]);
使用标题接受json,如下所示:

$response = $this->json('POST', '/users', ['contact' => '545678987'],
    ['Accept' => 'application/json']
);
对于验证错误,响应代码为422,因此使用:

if ($validator->fails()) {
    return response()->json($validator->errors(), 422);
}

我的服务覆盖了响应,是400。我将尝试头部接受,但响应是验证de信息结构。不仅仅是headerMy服务覆盖了响应,而且是400。我将尝试头部接受,但响应是验证de信息结构。不仅仅是头球