“Groovy API响应JSON对象断言”

huangapple go评论88阅读模式
英文:

Groovy API Response JSON object assertion

问题

API响应

    [数据:[重复发票引用:null,请求ID:null],错误:[[代码:API.INV.005,描述:发票引用为空]],状态:3,警告:null]

我将对错误对象进行断言,以下是我的代码

    def responseData = jsonSlurper.parseText(responseBody)
    assert responseData.Error.Description == "发票引用为空"
    
但是我收到了断言失败

    assert responseData.Error.Description == "发票引用为空"
           |            |     |           |
           |            |     |           false
           |            |     [发票引用为空]
           |            [[代码:API.INV.005,描述:发票引用为空]]
           [数据:[重复发票引用:null,请求ID:null],错误:[[代码:API.INV.005,描述:发票引用为空]],状态:3,警告:null]

请阐明断言部分的问题?
英文:

API Response

[Data:[DuplicateInvoiceReference:null, RequestID:null], Error:[[Code:API.INV.005, Description:Invoice Reference is null or empty]], 
Status:3, Warning:null]

I'm going to asset the Error Object, below is my code

def responseData = jsonSlurper.parseText(responseBody)
assert responseData.Error.Description == "Invoice Reference is null or empty"

but I received assertion failed

assert responseData.Error.Description == "Invoice Reference is null or empty"
       |            |     |           |
       |            |     |           false
       |            |     [Invoice Reference is null or empty]
       |            [[Code:API.INV.005, Description:Invoice Reference is null or empty]]
       [Data:[DuplicateInvoiceReference:null, RequestID:null], Error:[[Code:API.INV.005, Description:Invoice Reference is null or empty]], Status:3, Warning:null]

Please clarify the issues in the assertion part?

答案1

得分: 2

你似乎把 Description 当作 Error 是一个映射(map)来阅读。实际上,它是一个列表。

你应该这样写:

assert responseData.Error[0].Description == "Invoice Reference is null or empty"

你需要从 responseData.Error 的第一个元素中读取 Description

英文:

You're reading Description as though Error were a map. It is a list.

You should be having

assert responseData.Error[0].Description == "Invoice Reference is null or empty"

You need to read Description off the first element of responseData.Error

huangapple
  • 本文由 发表于 2020年8月25日 15:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定