From 42ea00e3097c238375ad1b3fb67bad67de3e4142 Mon Sep 17 00:00:00 2001 From: Christian Noon Date: Sun, 6 Sep 2015 11:45:11 -0700 Subject: [PATCH] Refactored the download tests to DRY them up a bit. --- Tests/DownloadTests.swift | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Tests/DownloadTests.swift b/Tests/DownloadTests.swift index 86ae34088..31ceebb6b 100644 --- a/Tests/DownloadTests.swift +++ b/Tests/DownloadTests.swift @@ -69,6 +69,17 @@ class DownloadResponseTestCase: BaseTestCase { let searchPathDirectory: NSSearchPathDirectory = .CachesDirectory let searchPathDomain: NSSearchPathDomainMask = .UserDomainMask + let cachesURL: NSURL = { + let cachesDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first! + let cachesURL = NSURL(fileURLWithPath: cachesDirectory, isDirectory: true) + + return cachesURL + }() + + var randomCachesFileURL: NSURL { + return cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json") + } + func testDownloadRequest() { // Given let numberOfLines = 100 @@ -245,17 +256,11 @@ class DownloadResponseTestCase: BaseTestCase { func testDownloadRequestWithParameters() { // Given - let fileURL: NSURL = { - let cachesDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first! - let cachesURL = NSURL(fileURLWithPath: cachesDirectory, isDirectory: true) - let fileURL = cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json") - - return fileURL - }() - + let fileURL = randomCachesFileURL let URLString = "https://httpbin.org/get" let parameters = ["foo": "bar"] let destination: Request.DownloadFileDestination = { _, _ in fileURL } + let expectation = expectationWithDescription("Download request should download data to file: \(fileURL)") var request: NSURLRequest? @@ -293,17 +298,11 @@ class DownloadResponseTestCase: BaseTestCase { func testDownloadRequestWithHeaders() { // Given - let fileURL: NSURL = { - let cachesDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first! - let cachesURL = NSURL(fileURLWithPath: cachesDirectory, isDirectory: true) - let fileURL = cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json") - - return fileURL - }() - + let fileURL = randomCachesFileURL let URLString = "https://httpbin.org/get" let headers = ["Authorization": "123456"] let destination: Request.DownloadFileDestination = { _, _ in fileURL } + let expectation = expectationWithDescription("Download request should download data to file: \(fileURL)") var request: NSURLRequest?