如何使用if条件检查其他值(使用空手道框架)? [英] How to check additional values with if condition (using karate framework)?

查看:84
本文介绍了如何使用if条件检查其他值(使用空手道框架)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用json模式检查来自GET/birds请求的响应.在我的功能中:

* def abs = read (birds.json)
* match response == abs.birdsSchema

我需要将架构放入json文件而不是功能中. 我必须根据性别检查其他值.例如:如果性别是男性,则检查颜色是蓝色,尾巴是长还是短.如果性别是女性,则检查唱歌"是对还是错以及卵数.

所以我放了birds.json:

"birdsSchema":{
    "id": "#string",
    "owner": "#number",
    "town": "#? _ == 'New York' || _ == 'Washington'",
    "type": "object",
    "oneOf": [
        {
            "properties": {
                "gender": {"enum": ["male"]},
                "color":"blue",
                "tail": "#? _ == 'long' || _ == 'short'"
            }
        },
        {
            "properties": {
                "gender": {"enum": ["female"]},
                "sings" : "#? _ == true || _ == false"
                "eggs": "##number"
            }
        }
    ]
}

但是它不起作用.错误:com.intuit.karate.exception.KarateException:路径:$ [0] .type,实际:女性",预期:对象",原因:不相等. 如何在我的json文件中执行此条件检查?

解决方案

让我们承认这非常困难,因为如果我正确理解了您的问题,那么您正在寻找的JSON 是动态的. /p>

空手道的乐趣之一是,我可以想到至少5种不同的方式来优雅地解决这个问题.这只是一个:

* def response = { color: 'black', aaa: 'foo' }

* def schema = { color: '#? _ == "black" || _ == "white"' }
* def extra = (response.color == 'black' ? { aaa: '#string' } : { fff: '#string' })

* match response contains schema
* match response contains extra

如果根据以上提示创建JS函数,则可能会得到更好的解决方案.请记住,在JS函数中,您可以使用karate.set之类的方法动态创建密钥.因此,有很多可能性:)

edit:看起来上面的示例是错误的,并且键不是动态的.然后很容易,请记住$指的是JSON根:

* def response = { color: 'black', extra: 'foo' }    
* def schema = { color: '#? _ == "black" || _ == "white"', extra: '#($.color == "black" ? "foo" : "bar")' }    
* match response == schema

I would like to check response from GET/birds request with a json schema. In my feature:

* def abs = read (birds.json)
* match response == abs.birdsSchema

I need to put the schema in a json file and not in the feature. I have to check additional values depending on gender. Ex: if gender is male then check if the color is blue and the tail is long or short. if gender is female then check if "sings" is true or false and number of eggs.

So I put in birds.json:

"birdsSchema":{
    "id": "#string",
    "owner": "#number",
    "town": "#? _ == 'New York' || _ == 'Washington'",
    "type": "object",
    "oneOf": [
        {
            "properties": {
                "gender": {"enum": ["male"]},
                "color":"blue",
                "tail": "#? _ == 'long' || _ == 'short'"
            }
        },
        {
            "properties": {
                "gender": {"enum": ["female"]},
                "sings" : "#? _ == true || _ == false"
                "eggs": "##number"
            }
        }
    ]
}

But it doesn't work. Error: com.intuit.karate.exception.KarateException: path: $[0].type, actual: 'female', expected: 'object', reason: not equal. How I can do this conditional check in my json file?

解决方案

Let's acknowledge that this is extra hard because if I understand your question correctly, the JSON keys you are looking for are dynamic.

Part of the fun of Karate is that there are at least 5 different ways I can think of to solve this elegantly. Here is just one:

* def response = { color: 'black', aaa: 'foo' }

* def schema = { color: '#? _ == "black" || _ == "white"' }
* def extra = (response.color == 'black' ? { aaa: '#string' } : { fff: '#string' })

* match response contains schema
* match response contains extra

If you create a JS function based on the hint above, you can probably get a better solution. Keep in mind that in a JS function you can use methods like karate.set to dynamically create keys. So there are many possibilities :)

edit: looks like the example above is wrong, and the keys are not dynamic. Then it is easy, keep in mind that $ refers to the JSON root:

* def response = { color: 'black', extra: 'foo' }    
* def schema = { color: '#? _ == "black" || _ == "white"', extra: '#($.color == "black" ? "foo" : "bar")' }    
* match response == schema

这篇关于如何使用if条件检查其他值(使用空手道框架)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆