我如何 运行 测试通过请求发送的 json 对象数组?
How do I run a test for an array of json objects sent through the request?
我正在通过请求传递一组 json 个对象,以便在控制器中进行验证:
控制器
public function store(Request $request)
{
$this->validate($request, [
'situation' => 'required',
]);
$emotions= ['data' => $request->input('emotions')];
$emotionsValidation = \Illuminate\Support\Facades\Validator::make($emotions, [
'data.*.id' => 'integer|between:1,70',
'data.*.intensity' => 'integer|between:0,100',
'data.*.new_intensity' => 'integer|between:0,100'
]);
if($emotionsValidation->fails()) {
return response(['error' => 'There was a problem with the emotions you submitted.'],422);
}
$emotions= ['data' => $request->input('emotions')];
$emotionsValidation = \Illuminate\Support\Facades\Validator::make($emotions, [
'data.*.id' => 'integer|between:1,70',
'data.*.intensity' => 'integer|between:0,100',
'data.*.new_intensity' => 'integer|between:0,100'
]);
if($emotionsValidation->fails()) {
return response(['error' => 'There was a problem with the emotions you submitted.'],422);
}
//Return record resource
}
验证工作正常,但测试未按应有的方式通过:
测试
/** @test */
public function a_records_json_emotions_must_be_integers()
{
$this->actingAs($creator = factory('App\User')->create());
$thought = [
'user_id' => 1,
'situation' => 'A situation',
'emotions' => json_decode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]")
];
//dd(json_encode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]"));
$response = $this->json('POST', '/api/thought/record', $thought )
->assertStatus(422);
}
当我取消注释 dd(json_encode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]"));
时,我得到:""[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]""
测试消息
- Tests\Feature\UserThoughtRecordsTest::a_thought_records_json_emotions_must_be_integers
Expected status code 422 but received 201.
Failed asserting that 422 is identical to 201.
我怎样才能通过这个测试?
json_decode()
将 return null
如果您在双引号字符串中使用单引号,如下所示:
json_decode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]")
您需要做的是对字符串使用单引号,对 JSON 使用双引号,如下所示:
json_decode('[{"id": 2000, "intensity": 100, "new_intensity": 20}]', true)
Laravels 验证可以将您的请求验证为 PHP 数组。
我正在通过请求传递一组 json 个对象,以便在控制器中进行验证:
控制器
public function store(Request $request)
{
$this->validate($request, [
'situation' => 'required',
]);
$emotions= ['data' => $request->input('emotions')];
$emotionsValidation = \Illuminate\Support\Facades\Validator::make($emotions, [
'data.*.id' => 'integer|between:1,70',
'data.*.intensity' => 'integer|between:0,100',
'data.*.new_intensity' => 'integer|between:0,100'
]);
if($emotionsValidation->fails()) {
return response(['error' => 'There was a problem with the emotions you submitted.'],422);
}
$emotions= ['data' => $request->input('emotions')];
$emotionsValidation = \Illuminate\Support\Facades\Validator::make($emotions, [
'data.*.id' => 'integer|between:1,70',
'data.*.intensity' => 'integer|between:0,100',
'data.*.new_intensity' => 'integer|between:0,100'
]);
if($emotionsValidation->fails()) {
return response(['error' => 'There was a problem with the emotions you submitted.'],422);
}
//Return record resource
}
验证工作正常,但测试未按应有的方式通过:
测试
/** @test */
public function a_records_json_emotions_must_be_integers()
{
$this->actingAs($creator = factory('App\User')->create());
$thought = [
'user_id' => 1,
'situation' => 'A situation',
'emotions' => json_decode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]")
];
//dd(json_encode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]"));
$response = $this->json('POST', '/api/thought/record', $thought )
->assertStatus(422);
}
当我取消注释 dd(json_encode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]"));
时,我得到:""[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]""
测试消息
- Tests\Feature\UserThoughtRecordsTest::a_thought_records_json_emotions_must_be_integers Expected status code 422 but received 201. Failed asserting that 422 is identical to 201.
我怎样才能通过这个测试?
json_decode()
将 return null
如果您在双引号字符串中使用单引号,如下所示:
json_decode("[{'id': 2000, 'intensity': 100, 'new_intensity': 20}]")
您需要做的是对字符串使用单引号,对 JSON 使用双引号,如下所示:
json_decode('[{"id": 2000, "intensity": 100, "new_intensity": 20}]', true)
Laravels 验证可以将您的请求验证为 PHP 数组。