Last active
February 7, 2018 16:14
-
-
Save ccabanero/9f7ad6775eacec3cc997 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Testing a Segue is called using a Mock UIViewController
This file contains hidden or 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
| // Sample 1: Calling segue to transition to another ViewController by tapping a UICollectionView cell | |
| func testSUT_CanTransitionToTargetViewController_WhenCollectionViewCellTapped() { | |
| // declare mock | |
| class MockViewController: ViewController { | |
| var segueIdentifier: NSString? | |
| override private func performSegueWithIdentifier(identifier: String, sender: AnyObject?) { | |
| segueIdentifier = identifier | |
| } | |
| } | |
| // instantiate mock | |
| let mockViewController = MockViewController() | |
| // assign a place array to mock (instead of calling network) - so the CollectionView has data | |
| let responseArray = [ | |
| [ | |
| "id": 16202, | |
| "lat": 42.1172689197255, | |
| "lng": -70.9595081595093, | |
| "name": "Abington", | |
| "state": "Massachusetts", | |
| "image": "https://s3.amazonaws.com/to-app-photos/place-null.png", | |
| "bestfits": 0, | |
| "favorite": false, | |
| "dismissed": false | |
| ], | |
| [ | |
| "id": 16750, | |
| "lat": 39.4250075233965, | |
| "lng": -74.4990416738654, | |
| "name": "Absecon", | |
| "state": "New Jersey", | |
| "image": "https://s3.amazonaws.com/to-app-photos/place-null.png", | |
| "bestfits": 0, | |
| "favorite": false, | |
| "dismissed": false | |
| ] | |
| ] | |
| mockViewController.items = responseArray | |
| // simulate tapping collection view cell | |
| mockViewController.collectionView(systemUnderTest.collectionView, didSelectItemAtIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
| // confirm segue was called | |
| let expectedSegueIdentifier = "toDetailsVC" // target segue identifier | |
| let actualSequeIdentifier = mockViewController.segueIdentifier | |
| XCTAssertEqual(expectedSegueIdentifier, actualSequeIdentifier) | |
| } | |
| // Sample 1: Calling segue to transition to another ViewController after logging in | |
| func testSUT_CanTransitionToTargetViewControllerAfterSuccessfulLogin() { | |
| // mock model class provides a successful login | |
| class MockUser: User { | |
| override func loginWithUsername(username: String, password: String, completion: (isSuccess: Bool, isNewUser: Bool) -> Void) { | |
| completion(isSuccess: true, isNewUser: false) | |
| } | |
| } | |
| // mock controller for evaluating what segue identifier was called after calling controller method | |
| class MockLoginViewController: LoginViewController { | |
| var segueIdentifier: NSString? | |
| override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) { | |
| segueIdentifier = identifier | |
| } | |
| } | |
| // LoginVC, SeedUser state | |
| let mockLoginVC = MockLoginViewController() | |
| let mockUser = MockUser() | |
| mockLoginVC.appUser = mockUser | |
| // TextFields have been entered | |
| let userTextField = UITextField() | |
| let passwordTextField = UITextField() | |
| mockLoginVC.userNameTextField = userTextField | |
| mockLoginVC.passwordTextField = passwordTextField | |
| mockLoginVC.userNameTextField.text = "bla@email.com" | |
| mockLoginVC.passwordTextField.text = "bla" | |
| // logging in succeeds | |
| mockLoginVC.handleLogInTap(UIButton()) // this calls model class to handle login (which will succeed) | |
| // Assertions | |
| XCTAssertNotNil(mockLoginVC.segueIdentifier) | |
| let expectedSegueIdentifier = "fromLoginVCtoTargetVC" // target segue identifier on successful login | |
| let actualSequeIdentifier = mockLoginVC.segueIdentifier | |
| XCTAssertEqual(expectedSegueIdentifier, actualSequeIdentifier) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment