尝试验证空手道的响应数据



我的回答

{
"result": {
"metadata": {
"company_employee": [
"94acdcbf-9c63-4fb8-939a-748fd0c8474d",
"c7d67a49-6502-4738-8c2e-3842312524bf",
"f7843e21-3b74-4935-b941-55756d58570c",
"5ee8120f-738b-4bf8-b7cc-f96271e876b6",
"0f6e06b9-1244-416c-a674-9e5081f3137d",
"805ddabd-a1ca-4296-8804-dd87f3311702",
"6454f8db-9802-4095-96d9-983029430d87",
"0733cf86-c42d-4043-a0bf-35c6bacea355"
]
},
"address": "Dhaka",
"is_active": true,
"gender": "male",
"created": "2021-01-18T10:53:07.898Z",
"date_of_birth": 0,
"user_name": "st_jahid4",
"profile_pic": null,
"last_name": "Jahid",
"contact": "",
"is_public": true,
"id": "20e776b7-523d-4592-9150-1127f9ac4cf1",
"first_name": "Md.",
"updated": "2021-08-12T11:56:22.641Z",
"email": "canon88803@vss6.com"
},
"code": 200,
"status": "ok"
}

模式:

* def refmetadata = {company_employee : '#[] uuid'}
* def refresult = 
"""
{ 
metadata: '#object refmetadata', 
address: '#string',
is_active:'#boolean',
gender:'#string',
created:'#string',
date_of_birth:'##null',
user_name:'#string',
profile_pic:'##null',
last_name:'#string',
contact:'#string',
is_public:'#boolean',
id:'#uuid',
first_name:'#string',
updated:'#string',
email:'#string'
}
"""
* def responseData = 
"""
{
result: '#object refresult',
code: '#number',
status: '#string'
}
"""

验证代码:

* string jsonData = response
* match jsonData  == responseData

怎么了?

多重问题。注意,您的示例太复杂了,所以我不打算花时间实际运行它。请花时间阅读文档和示例:https://github.com/intuit/karate#schema-validation

* def response =
"""
{
"result": {
"metadata": {
"company_employee": [
"94acdcbf-9c63-4fb8-939a-748fd0c8474d"
]
},
"address": "Dhaka",
},
"code": 200,
"status": "ok"
}
"""
* def meta = { company_employee : '#[] #uuid' }
* match response.result.metadata == meta
* def result = 
"""
{ 
metadata: '#(meta)', 
address: '#string'
}
"""
* def schema = 
"""
{
result: '#(result)',
code: '#number',
status: '#string'
}
"""
* match response == schema

最新更新