首页 > 解决方案 > 为什么模拟响应无法解码?

问题描述

我正在尝试使用模拟数据测试 api 调用,但出现以下错误。我声明的模拟响应应该被解码,以便处理程序可以正确地将数据传递给调用者,但它不允许我这样做,因为模拟响应应该是数据类型。我不确定如何让我的模拟响应成为 Data 类型,而不会给我更多错误。

在此处输入图像描述

 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 {
            let decoded = try self.decoder.decode([Comment].self, from: mockCommentsResponse)
           
            completionHandler(.success(decoded))
        }
        
    }

标签: swift

解决方案


.decode期望Data作为它的第二个参数。你提供[String:Any].

一种选择是将您的模拟结构转换为有效的 JSON 并使用数据(使用:.utf8)`:

let mockCommentsResponse = """
[
        {
          "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(using: .utf8)!

另一种选择是将您定义mockCommentsResponse为一个实际的数组,Comment然后使用它们将它们编码为 JSON/Data JSONEncoder,然后再次解码它们。但是,这可能会引发关于在这种情况下实际测试的内容的问题。


推荐阅读