空手道匹配每个响应断言无法识别响应中丢失的键

Karate match each on response assertion is failing to identify missing keys in response

我的代码中的每个断言都有一个匹配项,如下所示。刚刚尝试创建与我的代码类似的示例,只是为了解释这个问题。

  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,如果我想检查响应下每个对象中具有 'training' 的技能数组怎么办?我怎样才能做到这一点?

您可以使用 match each 来验证 json 模式 https://github.com/intuit/karate#match-each

请注意,response[*].password 是一个 JsonPath 表达式,它将 return 找到所有 password 键值的数组,并且 return 在您的情况下只有 3 个。

你要找的是这个:

* match each response contains { password: 'abc123' }