当我想在单元测试中测试数组时,我遇到了一个小问题。
我想测试键的结构和类型,但是我不知道如何处理它(我试过了,我保证!)
以下是json输入:
{
"success": true,
"data": [
{
"id": 1,
"domains_id": 1,
"sub": "",
"type": "",
"ip_or_fqdn": "",
"created_at": "2022-05-14T08:30:18.000000Z",
"updated_at": "2022-05-14T08:30:18.000000Z"
}
],
"message": "Domain retrieved successfully."
}目前,有一项测试是:
it('fetch zone entries [GET] with json response and check response type', function () {
TestCase::initDatabase();
Passport::actingAs(
User::factory()->make()
);
$response = $this->withHeaders([
'Accept' => 'application/json'
])
->json('GET', '/api/zone')
->assertStatus(200)
->assertJson(function (AssertableJson $json) {
$json->has('success')
->whereType('success', 'boolean')
->has('data')
->whereType('data', 'array')
->has('message')
->whereType('message', 'string');
});
TestCase::resetDatabase();
});我想用这个过程来测试“数据”数组键/值,当然,在这个闭包中也是如此;但是它有可能吗?
发布于 2022-05-15 08:16:08
例如,可以使用点表示法。
->assertJson(fn (AssertableJson $json) =>
$json->has('data.id')
->where('data.id', 1)
->missing('data.x')
);发布于 2022-05-15 17:56:12
最后,使用@ajthinking技巧,这是最后的测试,它是有效的,非常感谢!
it('fetch zone entries [GET] with json response and check response type', function () {
TestCase::initDatabase();
Passport::actingAs(
User::factory()->make()
);
$response = $this->withHeaders([
'Accept' => 'application/json'
])
->json('GET', '/api/zone')
->assertStatus(200)
->assertJson(function (AssertableJson $json) {
$json->has('success')
->whereType('success', 'boolean')
->has('data')
->whereType('data', 'array')
->has('data.0')
->has('data.0')
->has('data.0.id')
->has('data.0.sub')
->has('data.0.type')
->has('data.0.ip_or_fqdn')
->has('data.0.created_at')
->has('data.0.updated_at')
->whereType('data.0.id', 'integer')
->whereType('data.0.sub', 'string')
->whereType('data.0.type', 'string')
->whereType('data.0.ip_or_fqdn', 'string')
->whereType('data.0.created_at', 'string')
->whereType('data.0.updated_at', 'string')
->has('message')
->whereType('message', 'string');
});
TestCase::resetDatabase();
});将来,我将使用assertJsonStructure()来改进这个测试,以便在assertJson()闭包中测试基本结构和测试类型。
编辑:
下面是使用assertJsonStructure()方法进行的测试,它运行得很好:
it('fetch zone entries [GET] with json response and check response type', function () {
TestCase::initDatabase();
Passport::actingAs(
User::factory()->make()
);
$response = $this->withHeaders([
'Accept' => 'application/json'
])
->json('GET', '/api/zone')
->assertStatus(200)
->assertJsonStructure([
'success',
'data' => [
'*' => [
'id',
'sub',
'type',
'ip_or_fqdn',
'created_at',
'updated_at'
]
],
'message'
])
->assertJson(function (AssertableJson $json) {
$json->whereType('success', 'boolean')
->whereType('data', 'array')
->whereType('data.0.id', 'integer')
->whereType('data.0.sub', 'string')
->whereType('data.0.type', 'string')
->whereType('data.0.ip_or_fqdn', 'string')
->whereType('data.0.created_at', 'string')
->whereType('data.0.updated_at', 'string')
->whereType('message', 'string');
});
TestCase::resetDatabase();
});https://stackoverflow.com/questions/72244362
复制相似问题