如何使用 completionHandler 为 downloadTask 编写单元测试?
How to write unit test for `downloadTask` with `completionHandler `?
这是我的下载功能:
// Download a file from the url to the local directory
class func downloadUrl(url: URL, to dirUrl: URL, completion: (() -> ())?){
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url: url)
let task = session.downloadTask(with: request) {(tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success, copy the downloaded file from the memory to the disk
print("Finished downloading!")
do {
try FileManager.default.copyItem(at: tempLocalUrl,to:
dirUrl.appendingPathComponent((response?.suggestedFilename)!))
if completion != nil {
completion!()
}
} catch (let writeError) {
print("Fliled to write file \(dirUrl) : \(writeError)")
}
} else {
print("Failure: \(String(describing: error?.localizedDescription))")
}
}
task.resume()
}
我想写一个单元测试方法来测试它是否将文件从 url
下载到 dirUrl
。
func testDownloadUrl(){
let fm = FileManager.default
let url = URL(string: "https://raw.githubusercontent.com/apple/swift/master/README.md")
fileDownloader.downloadUrl(url: url!, to: fm.temporaryDirectory, completion: nil)
// Check the contents of temp file
let tempContents = try? fm.contentsOfDirectory(atPath: fm.temporaryDirectory.path)
print("Contents: \(tempContents)")
}
然而,即使我通过了单元测试,也没有输出"Finished downloading!"或"Failure ...",所以我猜在这个测试用例中没有调用completionHandler。
我的问题是如何让单元测试方法等到下载任务完成?
标准的习语是使用Xcode asynchronous testing APIs, mostly the XCTestExpectation
class。这些从 Xcode 6 开始可用,因此,如果您使用的是最新版本,您应该被覆盖 ;)
通用异步测试模板如下:
func testAsyncFunction() {
let asyncDone = expectation(description: "Async function")
...
someAsyncFunction(...) {
...
asyncDone.fulfill()
}
wait(for: [asyncDone], timeout: 10)
/* Test the results here */
}
这将阻止您的测试执行,直到上述功能完成(或指定的超时已过)。
这是我的下载功能:
// Download a file from the url to the local directory
class func downloadUrl(url: URL, to dirUrl: URL, completion: (() -> ())?){
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url: url)
let task = session.downloadTask(with: request) {(tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success, copy the downloaded file from the memory to the disk
print("Finished downloading!")
do {
try FileManager.default.copyItem(at: tempLocalUrl,to:
dirUrl.appendingPathComponent((response?.suggestedFilename)!))
if completion != nil {
completion!()
}
} catch (let writeError) {
print("Fliled to write file \(dirUrl) : \(writeError)")
}
} else {
print("Failure: \(String(describing: error?.localizedDescription))")
}
}
task.resume()
}
我想写一个单元测试方法来测试它是否将文件从 url
下载到 dirUrl
。
func testDownloadUrl(){
let fm = FileManager.default
let url = URL(string: "https://raw.githubusercontent.com/apple/swift/master/README.md")
fileDownloader.downloadUrl(url: url!, to: fm.temporaryDirectory, completion: nil)
// Check the contents of temp file
let tempContents = try? fm.contentsOfDirectory(atPath: fm.temporaryDirectory.path)
print("Contents: \(tempContents)")
}
然而,即使我通过了单元测试,也没有输出"Finished downloading!"或"Failure ...",所以我猜在这个测试用例中没有调用completionHandler。
我的问题是如何让单元测试方法等到下载任务完成?
标准的习语是使用Xcode asynchronous testing APIs, mostly the XCTestExpectation
class。这些从 Xcode 6 开始可用,因此,如果您使用的是最新版本,您应该被覆盖 ;)
通用异步测试模板如下:
func testAsyncFunction() {
let asyncDone = expectation(description: "Async function")
...
someAsyncFunction(...) {
...
asyncDone.fulfill()
}
wait(for: [asyncDone], timeout: 10)
/* Test the results here */
}
这将阻止您的测试执行,直到上述功能完成(或指定的超时已过)。