I want to test NSURLSession but I get the error "resume cannot be sent to abstract instance of class NSURLSessionDataTask".
Example code is in https://github.com/stevencurtis/abstract-instanceofclassNSURLSessionDataTask
My HTTP Manager works fine:
class HTTPManager {
static let shared: HTTPManager = HTTPManager()
private let session: URLSessionProtocol
init(session: URLSessionProtocol = URLSession.shared) {
self.session = session
}
public func get(urlString: String, completionBlock: ((Data?) -> Void)?) {
let url = URL(string: urlString)
if let usableUrl = url {
let request = URLRequest(url: usableUrl)
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
(completionBlock?(data))!
})
task.resume()
}
}
}
However I try to mock it
class MockURLSession: URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return URLSessionDataTask()
}
var searchedURL = URL(string: "asd")
}
And then create the test:
func testGetRequest() {
let url = URL(string: "http://gojek-contacts-app.herokuapp.com/contacts.json")
let session = MockURLSession()
let sub = HTTPManager(session: session)
sub.get(urlString: "http://gojek-contacts-app.herokuapp.com/contacts.json", completionBlock: { [weak self] (data: Data?) -> Void in
print ("vc")
}
)
}
The tests errors with: resume cannot be sent to abstract instance of class NSURLSessionDataTask" and I can't think of a way around this error!
Aucun commentaire:
Enregistrer un commentaire