You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么mocked response无法被解码?测试API调用时要求为Data类型报错

根因

JSONDecoder.decode(_:from:) 方法要求第二个入参为Data类型,你当前传入的mockCommentsResponse是[[String: Any]]类型的字典数组,类型不匹配触发报错。

修复代码

修改getComments方法的异常处理和类型转换逻辑即可:

let mockCommentsResponse: [[String: Any]] = [
    [
      "requestCommentId": 22377,
      "installRequestId": 27236,
      "enteredByValue": "EVIQ",
      "requestComment": "Delivery Date added by EVIQ",
      "creationTs": "2021-09-17 13:00:08",
      "modificationTs": "2021-09-17 13:00:08",
      "commentType": "System"
    ],
    [
      "requestCommentId": 22372,
      "installRequestId": 27236,
      "enteredByValue": "Customer",
      "requestComment": "Form Submitted by Customer",
      "creationTs": "2021-09-17 12:32:19",
      "modificationTs": "2021-09-17 12:32:19",
      "commentType": "System"
    ],
    [
      "requestCommentId": 22371,
      "installRequestId": 27236,
      "enteredByValue": "Customer",
      "requestComment": "Awaiting Form Completion",
      "creationTs": "2021-09-17 12:31:17",
      "modificationTs": "2021-09-17 12:31:17",
      "commentType": "System"
    ],
    [
      "requestCommentId": 22370,
      "installRequestId": 27236,
      "enteredByValue": "usa_dealer",
      "requestComment": "Sent VIN to Maritz",
      "creationTs": "2021-09-17 12:29:34",
      "modificationTs": "2021-09-17 12:29:34",
      "commentType": "System"
    ]
]

func getComments(requestId:String, completionHandler: @escaping (Result<[Comment], Error>) -> Void) {
    if shouldReturnError {
        completionHandler(.failure(MockServiceError.getComments))
    } else {
        do {
            // 第一步:将字典数组序列化为JSON格式的Data
            let jsonData = try JSONSerialization.data(withJSONObject: mockCommentsResponse)
            // 第二步:将Data解码为目标模型数组
            let decoded = try self.decoder.decode([Comment].self, from: jsonData)
            completionHandler(.success(decoded))
        } catch {
            // 捕获序列化、解码环节的所有错误
            completionHandler(.failure(error))
        }
    }
}

优化建议

如果不需要动态修改mock数据,可以直接将mock响应定义为JSON字符串,提前转成Data存储,避免每次调用都执行序列化操作,性能更优:

// 直接定义JSON格式的mock响应字符串
let mockCommentsJSON = """
[
    {
      "requestCommentId": 22377,
      "installRequestId": 27236,
      "enteredByValue": "EVIQ",
      "requestComment": "Delivery Date added by EVIQ",
      "creationTs": "2021-09-17 13:00:08",
      "modificationTs": "2021-09-17 13:00:08",
      "commentType": "System"
    },
    {
      "requestCommentId": 22372,
      "installRequestId": 27236,
      "enteredByValue": "Customer",
      "requestComment": "Form Submitted by Customer",
      "creationTs": "2021-09-17 12:32:19",
      "modificationTs": "2021-09-17 12:32:19",
      "commentType": "System"
    },
    {
      "requestCommentId": 22371,
      "installRequestId": 27236,
      "enteredByValue": "Customer",
      "requestComment": "Awaiting Form Completion",
      "creationTs": "2021-09-17 12:31:17",
      "modificationTs": "2021-09-17 12:31:17",
      "commentType": "System"
    },
    {
      "requestCommentId": 22370,
      "installRequestId": 27236,
      "enteredByValue": "usa_dealer",
      "requestComment": "Sent VIN to Maritz",
      "creationTs": "2021-09-17 12:29:34",
      "modificationTs": "2021-09-17 12:29:34",
      "commentType": "System"
    }
]
"""
// 提前转为Data存储,调用时直接使用
let mockCommentsData = Data(mockCommentsJSON.utf8)

内容的提问来源于stack exchange,提问作者user9650660

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 09:54:03