Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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