[英]Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'
I created a function to retrieve json data from a network server. 我创建了一个从网络服务器检索json数据的函数。 It works properly when run in a production environment but when it's setup to be unit-tested the error :
Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'
. 它在生产环境中运行时可以正常工作,但是将其设置为进行单元测试时出现错误:
Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'
。 Here is the function: 这是函数:
func getJSONData <T : Codable> (url: ServerUrl, type: T.Type, completionHandler:@escaping (_ details: T) -> ()) {
guard let url = URL(string: baseURL + url.rawValue) else { return }
session.dataTask(with: url, completionHandler: {
(data, response, err) in
guard err == nil else {
print("There was a network error:", err as Any)
return
}
guard let data = data else { return }
do {
let updatedModels = try JSONDecoder().decode(T.self, from: data)
completionHandler(updatedModels)
} catch let jsonErr {
print("Error serializing json:", jsonErr)
return }
}).resume()
}
Here it is in the unit test: 这是在单元测试中:
func test_getJSONData_should_return_data() {
var actualData: Data?
sut.getJSONData(url: ServerUrl.colors, type: MainColor.self, completionHandler: { (data) in //error here
actualData = data
})
XCTAssertNotNil(actualData)
} }
问题是MainColor
返回类型MainColor
值不是Data
,这应该是
var actualData: MainColor?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.