Testing asynchronous code in Swift with XCTest expectations

Testing asynchronous callbacks in your Swift code? Try this:

let expectation = expectationWithDescription("Get some sandwiches!")

let client = SandwichClient()

client.getSandwiches { result in 
    switch result {
    case .Success:
        expectation.fulfill() // ?
    case .Failure:
        XCTFail("Expected getSandwiches to succeed, but it failed. ?")

waitForExpectationsWithTimeout(10, handler: nil)

Consider using OHHTTPStubs or just stubbing manually to make this run faster.

(Note that result is a simple Result enum like Rob Napier describes in Functional Wish Fulfillment.)