我如何为空手道 DSL 中的模式断言提供动态密钥
How do i give a dynamic key for schema assertion in karate DSL
我想通过在架构断言中添加一个变量来匹配我的响应。我通过给出“#(value)”来尝试它,但它没有用
* def value = 3
Then match object ==
{
result : {
'#(value)'
{
firstName : '#string',
lastName : '#string'
}
}
}
我遇到的异常是“path $.result.(#value) actual : null expected {
名字:'#string'
姓氏:'#string'
}'
你的 JSON 出了点大问题。您真的要使用动态密钥吗?这不可能。
下面是一个可以帮助您找出问题所在的工作示例:
* def actual = { result: { value: 3, foo: { firstName: 'John', lastName: 'Smith' } } }
* def value = 3
Then match actual ==
"""
{
result : {
value: '#(value)',
foo: {
firstName : '#string',
lastName : '#string'
}
}
}
"""
(edit:) 看起来请求确实是一个动态密钥,这里是一个改变的例子:
* def actual = { result: { 3: { firstName: 'John', lastName: 'Smith' } } }
* def fun =
"""
function(key) {
var temp = { result: {} };
temp.result[key] = { firstName: '#string', lastName: '#string' };
return temp;
}
"""
Then match actual == fun(3)
我想通过在架构断言中添加一个变量来匹配我的响应。我通过给出“#(value)”来尝试它,但它没有用
* def value = 3
Then match object ==
{
result : {
'#(value)'
{
firstName : '#string',
lastName : '#string'
}
}
}
我遇到的异常是“path $.result.(#value) actual : null expected { 名字:'#string' 姓氏:'#string' }'
你的 JSON 出了点大问题。您真的要使用动态密钥吗?这不可能。
下面是一个可以帮助您找出问题所在的工作示例:
* def actual = { result: { value: 3, foo: { firstName: 'John', lastName: 'Smith' } } }
* def value = 3
Then match actual ==
"""
{
result : {
value: '#(value)',
foo: {
firstName : '#string',
lastName : '#string'
}
}
}
"""
(edit:) 看起来请求确实是一个动态密钥,这里是一个改变的例子:
* def actual = { result: { 3: { firstName: 'John', lastName: 'Smith' } } }
* def fun =
"""
function(key) {
var temp = { result: {} };
temp.result[key] = { firstName: '#string', lastName: '#string' };
return temp;
}
"""
Then match actual == fun(3)