Skip to content

Instantly share code, notes, and snippets.

@tapsandswipes
Created January 12, 2016 18:09
Show Gist options
  • Save tapsandswipes/26ae01329c08062bdbf2 to your computer and use it in GitHub Desktop.
Save tapsandswipes/26ae01329c08062bdbf2 to your computer and use it in GitHub Desktop.
XCTest: Testing async callbacks
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