空手道:将动态子响应与父API响应匹配 [英] Karate: Match a dynamic child response with the Parent API response

查看:45
本文介绍了空手道:将动态子响应与父API响应匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方案,其中父API响应的一部分来自子API.如果子API响应(动态的)只有一个数组元素,则无需与父API响应中的数组元素匹配,if array size > 1然后我需要从病区的索引1与父API响应进行匹配.

I have a scenario where a portion of the Parent API response is from a child API. If the child API response(which is dynamic) has only one array element then no need to match that in parent API response, if array size > 1 then I need to match from index 1 on wards with parent API response.

* def child =  {"array1":[{"mbr1":{"id":"A1"}},{"mbr2":{"id":"A2"}}]}

子api响应没有特定的顺序,并且array1可以具有"n"个数组元素(mbr1,mbr2,mbr3, etc)

There is no specific order for the child api response and array1 can have "n" number of array elements(mbr1,mbr2,mbr3, etc)

如果子API响应如上所示,则父API如下所示:

If the child API response is like above then the parent will look like below:

* def parent =  {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":[{"mbr2":{"id":"A2"}}]}]}

因此,仅在满足上述条件的情况下,才会向父API响应填充父API响应. 如果子API仅返回一个元素,则父API响应将如下所示:

So in parent API response towards the end child api response is populated, only if the above mentioned conditions are satisfied. If child API returns only one element then the parent API response will look like the below:

* def parent = {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]}

那么,如果子级返回200 OK并且子级数组长度> 1,该如何匹配以查看子级是否存在于父级中? 因此,我正在寻找以下情况的解决方案:

So how do I do a match to see whether the child is present in parent if child returns 200 OK and the child array length > 1? So I am looking for a solution for the below scenario:

if (child responseStatus == 200){
    if (child.array1.length > 1){
        for (i = 1;i <= child.array1.length; i++){
            parent.elg[1].members[i] contains child.arrays1[i]
        }
    }
}  

推荐答案

在这里:

* copy last = child.array1
* remove last[0]
* def multi = {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":"#(last)"}]}
* def expected = child.array1.length == 1 ? {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]} : multi
* match parent == expected

现在,我非常确定您(或团队中的其他任何人)将很难理解地球上的工作原理.这带给我我对您的建议.

Now I'm quite sure you (or anyone else in your team) will have a really hard time understanding how on earth that works. Which brings me to the advice I have for you.

大多数团队不会进行这种动态复杂的体操运动.那不是您应该进行测试的方式.

Most teams don't do this kind of dynamic complex gymnastics. That is not how you should approach tests.

您有2种情况.一种情况是您的孩子中有1个元素.还有其他情况.

You have 2 scenarios. One case where your have 1 element in child. And other cases.

请编写两个单独的Scenario -s,并确保传递给任何服务的数据均保证返回特定的形状.如果您无法执行此操作,则可能意味着它是如此动态,以至于您没有必要进行测试-也许它一开始就不应成为验证的重点.

Please write two separate Scenario-s and make sure the data you pass to whatever service is guaranteed to return a specific shape. If you can't do this, it probably means it is so dynamic, there is no point in you testing it - maybe it should not be the focus of your validation in the first place.

因此您的测试变得简单.

So your test becomes simple.

Scenario: first case
# send request1, get child and parent
# match parent == {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]}

Scenario: second case
# send request2, get child and parent
# match parent == {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":[{"mbr2":{"id":"A2"}}]}]}

有关如何简化不良测试的另一个示例,请参见: https://stackoverflow.com/a/54126724/143475

For another example of how to simplify bad tests, see this: https://stackoverflow.com/a/54126724/143475

这篇关于空手道:将动态子响应与父API响应匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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