尝试设置执行网络请求并尝试序列化响应的单元测试。我当前收到错误:对成员“jsonObject(with:options:)”的引用不明确
。很困惑为什么会发生这种情况,因为单元测试应该知道什么是 JSONSerialization。是?
func testAccessKeys() {
let expected = expectation(description: "Run the Access request")
sut.request(.Access, data: nil) { finished, response in
if response != nil && finished == true {
guard let json = try? JSONSerialization.jsonObject(with: response!, options: .mutableContainers) as! [String:Any] else { return XCTFail("Access request was not a dictionary")}
XCTAssertNotNil(json?["id"])
expected.fulfill()
} else {
XCTFail("Access response was nil")
}
}
waitForExpectations(timeout: 3) { error in
if let error = error {
XCTFail("Access request failure: \(error)")
}
}
}
最佳答案
确保响应的类型为Data
或InputStream
。
这些是此函数唯一接受的类型,如 documentation 中所示。
关于ios - Swift 4 对成员 'jsonObject(with:options:)' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729953/