我的代码中每个断言都有一个匹配项,如下所示。只是尝试创建与我的代码类似的示例,只是为了说明问题。
Scenario: Example scenario 1
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123"
},
{
id: 2,
name: "David",
password: "abc123"
},
{
id: 3,
name: "Mike",
password: "abc123"
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
测试状态:通过
对象4(其中id = 4)中缺少密码字段。上面的测试对我来说是合格的。我希望空手道在这种情况下无法通过测试。在这种情况下如何使测试失败?
Scenario: Example scenario 2
* def response =
"""
[
{
id: 1,
name: "John",
},
{
id: 2,
name: "David",
},
{
id: 3,
name: "Mike",
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
测试状态:通过
在这里,根本没有密码字段作为响应。但是我的测试通过了。
需要一种变通方法以使此类情况失败。
示例3:
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 2,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 3,
name: "David",
password: "abc123",
skills :[ "training", "coding"
]
},
{
id: 4,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
}
]
"""
考虑使用* match each response contains { password: 'abc123' }
格式(由@peter提及)来检查示例1和2,如果我想检查在响应中的每个对象中都有“训练”的技能数组怎么办?我该如何实现?
答案 0 :(得分:2)
您可以使用match each来验证json模式 https://github.com/intuit/karate#match-each
答案 1 :(得分:1)
请注意,response[*].password
是一个JsonPath表达式,它将返回找到的所有password
键值的数组,在您的情况下将仅返回3。
您正在寻找的是这个
* match each response contains { password: 'abc123' }