用hamcrest处理数组,放心 [英] Dealing arrays with hamcrest and rest assured
问题描述
我不知道如何使用 hamcrest 创建代码来检查具有这些属性的数组内的数组.
I can't figure out how to create the code using hamcrest to check an array inside array having these properties.
(想象一下,因为它有多个包含不同数据的条目)
(Imagine this as it has multiple entries with different data)
{
"mobilenum": "+6519829340",
"firstname": "Allen",
"lastname": "Edwards",
"location": "Singapore"
}
如果我使用这个:
.body("smsentries.mobilenum", contains(equalTo("+6519829340")));
它返回它确实存在,但是我如何进行更多检查以确保它找到的对象也具有相同的名字、姓氏和位置?
it returns that it does exist but how can I put more checks that the object it has found also has the same firstname, lastname and location?
我也认为这是错误的:
.body("smsentries.mobilenum", contains(equalTo("+6519829340")))
.and()
.body("smsentries.firstname", contains(equalTo("Allen"));
据我所知,如果数组包含等于提供的 mobilenum 并且数组包含名称Allen",它会搜索数组
As what I understand here is that it searches the array if the array contains mobilenum equal to what is provided and if the array contains the name "Allen"
我需要的是找到一个数组,其 mobilenum 等于+6519829340",名字等于Allen".
What I needed to is to find the array having the mobilenum equal to "+6519829340" and having the firstname equalto "Allen".
你们知道怎么做吗?
推荐答案
我需要的是找到具有等于 mobilenum 的数组+6519829340"并且名字等于Allen".
What I needed to is to find the array having the mobilenum equal to "+6519829340" and having the firstname equalto "Allen".
您可以使用查找"方法:
You can make use of the "find" method:
.body("smsentries.find { it.mobilenum == '+6519829340' }.firstname", equalTo("Allen")
.body("smsentries.find { it.mobilenum == '+6519829340' }.lastname", equalTo("Edwards").
如您所见,您实际上是在复制两种情况下的路径表达式,因此为了改进这一点,我们可以使用 根路径:
As you see you're essentially duplicating the path expression in the two cases so to improve this we can make use of root paths:
.root("smsentries.find { it.mobilenum == '+6519829340' }").
.body("firstname", equalTo("Allen")
.body("lastname", equalTo("Edwards").
您还可以参数化根路径:
You can also parameterize root paths:
.root("smsentries.find { it.mobilenum == '%s' }").
.body("firstname", withArgs("+6519829340"), equalTo("Allen")
.body("lastname", withArgs("+6519829340"), equalTo("Edwards").
.body("firstname", withArgs("+12345678"), equalTo("John")
.body("lastname", withArgs("+12345678"), equalTo("Doe").
这篇关于用hamcrest处理数组,放心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!