Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ccabanero/24a46c777bb29da95ba5 to your computer and use it in GitHub Desktop.
Save ccabanero/24a46c777bb29da95ba5 to your computer and use it in GitHub Desktop.
Sample iOS Integration Test: Test When Model performs work over the Network
// Example of an asynchronous unit test
func testUserLogin() {
let readyExpectation = expectationWithDescription("ready")
User.loginWithUsername("kris@bla.org", password: "blah") { (isSuccess) -> Void in
let expectedLoginSuccessStatus = true
let actualLoginSuccessStatus = isSuccess
XCTAssertEqual(expectedLoginSuccessStatus, actualLoginSuccessStatus, "Failed to login and provide the expected login status result")
readyExpectation.fulfill()
}
waitForExpectationsWithTimeout(5) { (error) -> Void in
XCTAssertNil(error, "Error when performing asynchronous unit test - testUserLogin")
}
}
// Other examples of fetching data from network with well formed URL and mal-formed URL
func testCanFetchData() {
let readyExpectation = expectationWithDescription("ready")
let someService = SomeService()
let url = "http://goodurl.com?id=12345" //well formed URL
someService.fetchData(url) { (data, error) -> Void in
XCTAssertNotNil(data)
XCTAssertNil(error)
readyExpectation.fulfill()
}
waitForExpectationsWithTimeout(5) { (error) -> Void in
XCTAssertNil(error)
}
}
func testCanHandleMalformedURLrWhenFetchingData() {
let readyExpectation = expectationWithDescription("ready")
let someService = SomeService()
let badURL = "\BLA&*()BLABLA" // a mal-formed URL
someService.fetchData(badURL) { (data, error) -> Void in
XCTAssertNil(data)
XCTAssertNotNil(error)
readyExpectation.fulfill()
}
waitForExpectationsWithTimeout(5) { (error) -> Void in
XCTAssertNil(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment