Skip to content

Instantly share code, notes, and snippets.

@kean
Last active January 9, 2016 20:20
Show Gist options
  • Save kean/c7adb3a70de179e72557 to your computer and use it in GitHub Desktop.
Save kean/c7adb3a70de179e72557 to your computer and use it in GitHub Desktop.
// The MIT License (MIT)
//
// Copyright (c) 2015 Alexander Grebenyuk (github.com/kean).
import XCTest
import Foundation
extension XCTestCase {
public func expect(block: (fulfill: (Void) -> Void) -> Void) {
let expectation = self.expectation()
block(fulfill: { expectation.fulfill() })
}
public func expectation() -> XCTestExpectation {
return self.expectationWithDescription("GenericExpectation")
}
public func expectNotification(name: String, object: AnyObject? = nil, handler: XCNotificationExpectationHandler? = nil) -> XCTestExpectation {
return self.expectationForNotification(name, object: object, handler: handler)
}
public func wait(timeout: NSTimeInterval = 2.0, handler: XCWaitCompletionHandler? = nil) {
self.waitForExpectationsWithTimeout(timeout, handler: handler)
}
}
// Usage:
func testThatDataTasksWithDifferentCachePolicyAreNotReused() {
let request1 = ImageRequest(URLRequest: NSURLRequest(URL: defaultURL, cachePolicy: .UseProtocolCachePolicy, timeoutInterval: 0))
let request2 = ImageRequest(URLRequest: NSURLRequest(URL: defaultURL, cachePolicy: .ReturnCacheDataDontLoad, timeoutInterval: 0))
self.expect { fulfill in
self.manager.taskWithRequest(request1) { _ in
fulfill()
}.resume()
}
self.expect { fulfill in
self.manager.taskWithRequest(request2) { _ in
fulfill()
}.resume()
}
self.wait { _ in
XCTAssertEqual(self.mockSessionManager.createdTaskCount, 2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment