SoapUI 的脚本断言对于 json 响应失败



我想检查occupanysequenceorder的每个实例是否匹配该字段输入的请求。当我进行log.error时,它会输出以下内容:

ERROR:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ERROR:1

因此,作为输入1的请求,这意味着在列表中,所有实例都需要等于1,而其上述则需要。但是,当我执行断言时:

assert roominfo.occupancySequenceOrder.flatten() == occupancysequenceorder_request

它引发了一个错误的断言,我不确定为什么?如何通过执行相关检查的脚本断言来通过它。我改变了主张 assert roominfo.occupancySequenceOrder.flatten().contains(occupancysequenceorder_request)及其通过,但我不确定这是否实际上可以进行正确的检查以确保occupanysequenceorder的每个实例都与输入的请求匹配。

以下是代码:

json.testregions.each { roominfo ->
   log.error roominfo.occupancySequenceOrder.flatten()
   log.error occupancysequenceorder_request
   assert roominfo.occupancySequenceOrder.flatten() == occupancysequenceorder_request
}

从此处查看OP的另一个问题及其数据

您可以在Script Assertion下面尝试:

//Check if the response is not empty
assert context.response, "Response is empty or null"
//Modify the value of the quest or read it thru properties if you want
def requestValue = 1
def json = new groovy.json.JsonSlurper().parseText(context.response)
json.regions.each { region ->
    region.hotels.each { hotel ->
        hotel.roomInformation. each { room ->
            assert room.occupancySequenceOrder == requestValue, "Value did not match for room ${room.hotelRoomId}"
        }
    }
}

而是尝试:

roominfo.occupancySequenceOrder.every { it == 1 }

flatten()对平面List没有影响。

您也可以尝试:

roominfo.occupancySequenceOrder.unique() == [1]

如果您想比较列表。