swift - 为什么模拟响应无法解码?
问题描述
我正在尝试使用模拟数据测试 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))
}
}
解决方案
.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
,然后再次解码它们。但是,这可能会引发关于您在这种情况下实际测试的内容的问题。
推荐阅读
- google-sheets - 日期之间的 VLookup,结合 sumproduct
- r - tbl() 在 sparklyr 查询中的作用是什么?
- database - 您会使用数字或语义标签作为列值吗?
- apache-kafka - 使用 Kafka-Connector 将数据从 Python-Opcua-Client 获取到 Kafka-topic 的可能性
- javascript - 输入标签,为什么我的 javascript 代码不起作用?
- python-2.7 - 为什么我的鼻子测试在使用继承时以 A、AB、ABC 方式重复测试
- aframe - 如何在框架中加载自定义字体?
- eclipse - 无法在 Eclipse Spring 引导项目中找到或加载主类
- javascript - 如何设置边缘跳过另一个边缘?
- pytorch - 如何将微调过的 bert 模型的输出作为输入提供给另一个微调过的 bert 模型?