Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccabanero/d3bc7c36b2b1381be97494404407ce26 to your computer and use it in GitHub Desktop.
Save ccabanero/d3bc7c36b2b1381be97494404407ce26 to your computer and use it in GitHub Desktop.
Unit Test Sample - Confirm that an API class requests data during a ViewController's viewDidLoad
func testSUT_UsesAPIClientToRequestModelData_WhenViewLoads() {
// declare mock of APIClient
class MockAPIClient: APIClient {
var modelObjectRequested: Bool!
override func fetchDataForId(id: Int, completion: (success: Bool, object: AnyObject?, message: String?) -> ()) {
// use property to confirm the data request was made
modelObjectRequested = true
let testPlace = MyModelObject(uniqueId: 100, city: "", state: "")
completion(success: true, object: testPlace, message: nil)
}
}
// declare mock of ViewController
class MockViewController: PlaceDetailsViewController {}
// instantiate mock of ViewController
let mockAPIClient = MockAPIClient()
let mockViewController = MockViewController()
mockViewController.apiClient = mockAPIClient
let _ = mockViewController.view
// confirm that fetchDataForId() was called when ViewController mock loaded it's view
XCTAssertTrue(mockAPIClient.placeObjectRequested)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment