3

如何使用放心的 .body() 方法在“描述”数组中声明我的属性。

例子:

 .body ("[0] .userType", equalTo (1)); // error 

这是我想要断言的当前 JSON 数据:

{
"validSession": true,
"value": "xxx",
"description": [
    {
        "userType": 1,
        "userTypeDescription": "xxx",
        "uname": "xx",
        "distributorId": 1
    }
]}
4

4 回答 4

4

我编辑它:

.body("validSession",is(true))
.body("description[0].userType", equalTo(1))
.body("description[0].userTypeDescription", containsString("xxx"))
.body("description[0].uname", containsString("xx"))
.body("description[0].distributorId", equalTo(1));

我测试了它并且有效。但我不明白为什么它只能通过将数组的所有元素的索引为零来工作。

你可以解释吗?

于 2018-02-26T12:01:24.097 回答
0

尝试使用以下代码片段:

.body("description[0]", hasItem(1))

让我知道它是否有帮助。

于 2018-02-26T10:41:22.427 回答
0

您还可以使用 Hamcrest:

Response data = httpClientRequest.getApiCall(url);
data.then().assertThat().body("description.userTypeDescription[0]", Is.is("xxx"));
于 2019-08-06T10:12:00.617 回答
0

你可以解释吗?

您需要description[0]在测试中引用的原因是 JSON 数据中的元素“描述”是一个数组。您正在使用数组语法来声明您打算读取名为“description”的数组的第一个元素。

于 2018-12-03T23:41:20.893 回答