Created
May 6, 2016 15:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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