【问题标题】:Karate match each on response assertion is failing to identify missing keys in response空手道匹配每个响应断言未能识别响应中缺少的键
【发布时间】:2020-07-14 00:04:38
【问题描述】:

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

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

【问题讨论】:

    标签: karate


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。是的,现在查看架构部分。完成架构部分后,我将接受答案。只需要澄清一下。在这种情况下,空手道不应该通过测试吗?在我看来,这可能是一个空手道错误。
    【解决方案2】:

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

    你要找的是这个:

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

    【讨论】:

    • 感谢您的回复。我同意你的观点。用示例 3 更新了问题。请帮助我了解如何使用您的匹配格式来实现这种情况。谢谢!
    • 好的,接受答案并创建了一个新问题(stackoverflow.com/questions/62880547/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多