我正在编写一些代码,以使测试更加容易。经过研究,我发现使URLSession
可测试的一个好方法是使其符合协议并在我需要测试的类中使用该协议。我对URL
应用了相同的方法。但是,我现在需要将url参数下调为URL类型。对我来说,这似乎有些“危险”。使URL
可测试的正确方法是什么?我该如何模拟URLSessionDataTask
返回的dataTask
类型?
import Foundation
protocol URLSessionForApiRequest {
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
}
protocol URLForApiRequest {
init?(string: String)
}
extension URLSession: URLSessionForApiRequest {}
extension URL: URLForApiRequest {}
class ApiRequest {
class func makeRequest(url: URLForApiRequest, urlSession: URLSessionForApiRequest) {
guard let url = url as? URL else { return }
let task = urlSession.dataTask(with: url) { _, _, _ in print("DONE") }
task.resume()
}
}
答案 0 :(得分:0)
您要创建一个协议,该协议包装要调用的函数,然后创建一个具体的实现和模拟实现,以返回使用其初始化的内容。这是一个示例:
import UIKit
import PlaygroundSupport
protocol RequestProvider {
func request(from: URL, completion: @escaping (Data?, URLResponse?, Error?) -> Void)
}
class ApiRequest: RequestProvider {
func request(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume
}
}
class MockApiRequest: RequestProvider {
enum Response {
case value(Data, URLResponse), error(Error)
}
private let response: Response
init(response: Response) {
self.response = response
}
func request(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
switch response {
case .value(let data, let response):
completion(data, response, nil)
case .error(let error):
completion(nil, nil, error)
}
}
}
class SomeClassThatMakesAnAPIRequest {
private let requestProvider: RequestProvider
init(requestProvider: RequestProvider) {
self.requestProvider = requestProvider
}
//Use the requestProvider here and it uses either the Mock or the "real" API provider based don what you injected
}