Testing asynchronous code in Swift with XCTest expectations

Datetime:2016-08-23 04:14:55         Topic: Swift          Share        Original >>
Here to See The Original Article!!!

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() // :tada:
    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 .)


Put your ads here, just $200 per month.