JSON Response
{
Feed[
{"item_type":"cycle","id":123},
{"item_type":"computer","name":"mac"},
{"item_type":"Bag","weight":"2"}
]
}
As nested schema is changing based on item type
so can we compare schema based on item type?
example: if item_type is cycle then it should compare with id,
if item_type is computer then it should compare name like this
我尝试过以下方法:
* def schema = {item_type:"#string",id:'##number',name: '##string',weight:'##string'}
在此架构验证中,即使item_type为cycle且名称为mac,测试用例也会通过,但实际上它应该会失败
答案 0 :(得分:3)
首先让我建议尝试进行这些极端的“通用”验证可能会浪费时间。查看此答案:https://stackoverflow.com/a/54126724/143475
但这是一种可能的解决方案,可能还有其他方法。
请阅读文档并研究其他答案以正确理解此内容。这将使用第二个功能文件。
第一个功能文件:
* def response =
"""
[
{ "item_type": "cycle", "id": 123 },
{ "item_type": "computer", "name": "mac" },
{ "item_type": "bag", "weight": "2" }
]
"""
* print response
* def data =
"""
{
cycle: { id: '#number' },
computer: { name: '#string' },
bag: { weight: '#string' }
}
"""
* def isValid = function(x){ karate.call('called.feature', x) }
* karate.forEach(response, isValid)
第二功能文件(称为called.feature
):
@ignore
Feature:
Scenario:
* def expected = data[__arg.item_type]
* match __arg contains expected