當我想在單元測驗中測驗一個陣列時,我遇到了一個小問題。
我想測驗鍵的結構和型別,但我不知道如何處理它(我試過了,我保證!)。
這是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();
});
我想用這個程序測驗“資料”陣列鍵/值,當然,在這個閉包中;但有可能嗎?
uj5u.com熱心網友回復:
例如,您可以使用點表示法
->assertJson(fn (AssertableJson $json) =>
$json->has('data.id')
->where('data.id', 1)
->missing('data.x')
);
uj5u.com熱心網友回復:
最后,有了@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://www.uj5u.com/qita/475071.html
下一篇:如何為DjangoRestFrameWork中具有兩個ManyToManyFields的模型撰寫__str__方法的測驗用例?
