Created
January 12, 2016 18:09
-
-
Save tapsandswipes/26ae01329c08062bdbf2 to your computer and use it in GitHub Desktop.
XCTest: Testing async callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import MyApp | |
class CallbackTest: XCTestCase { | |
func testAsyncCalback() { | |
let service = SomeService() | |
// 1. Define an expectation | |
let expectation = expectationWithDescription("SomeService does stuff and runs the callback closure") | |
// 2. Exercise the asynchronous code | |
service.doSomethingAsync { success in | |
XCTAssertTrue(success) | |
// Don't forget to fulfill the expectation in the async callback | |
expectation.fulfill() | |
} | |
// 3. Wait for the expectation to be fulfilled | |
waitForExpectationsWithTimeout(1) { error in | |
if let error = error { | |
XCTFail("waitForExpectationsWithTimeout errored: \(error)") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment