{"id":24050305,"url":"https://github.com/ccabanero/ios-unit-testing-patterns","last_synced_at":"2026-02-01T12:50:09.623Z","repository":{"id":20398551,"uuid":"23674492","full_name":"ccabanero/ios-unit-testing-patterns","owner":"ccabanero","description":"Unit testing code snippets for various iOS development scenarios.","archived":false,"fork":false,"pushed_at":"2017-12-31T16:28:00.000Z","size":50,"stargazers_count":164,"open_issues_count":0,"forks_count":34,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-01-09T01:55:36.917Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ccabanero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-04T18:33:25.000Z","updated_at":"2024-11-22T12:55:25.000Z","dependencies_parsed_at":"2022-07-25T07:47:02.359Z","dependency_job_id":null,"html_url":"https://github.com/ccabanero/ios-unit-testing-patterns","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ccabanero/ios-unit-testing-patterns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccabanero%2Fios-unit-testing-patterns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccabanero%2Fios-unit-testing-patterns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccabanero%2Fios-unit-testing-patterns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccabanero%2Fios-unit-testing-patterns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ccabanero","download_url":"https://codeload.github.com/ccabanero/ios-unit-testing-patterns/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ccabanero%2Fios-unit-testing-patterns/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275266508,"owners_count":25434598,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-15T02:00:09.272Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-09T01:55:03.946Z","updated_at":"2026-02-01T12:50:09.584Z","avatar_url":"https://github.com/ccabanero.png","language":null,"funding_links":[],"categories":["👨🏼‍💻 Checklists \u0026 Notes: \u003cbr/\u003e"],"sub_categories":["W \u003cbr/\u003e"],"readme":"iOS Unit Testing Patterns\n=========================\n\n### Description\n\nUnit testing code snippets for various iOS development scenarios.\n\n### Language\n\nSwift 4.x\n\n### Unit Testing Framework\n\nXCTest.framework\n\n### Sample Unit Tests for ViewController Classes\n\n__[Working with a ViewController composed of TableViews](https://gist.github.com/ccabanero/39ee6d5fd7b289dee695)__\n\n* View Controller is composed of TableView\n* Conforms to and implements UITableViewDataSource protocol methods.\n* Conforms to and implements UITableViewDelegate protocol methods.\n* TableView has expected reuse identifier\n* TableViewCells have expected properties (e.g. cell label)\n\n__[Working with a ViewController composed of CollectionViews](https://gist.github.com/ccabanero/6ef47c1aeb21acfb326d30f6b01825d1)__\n\n* Conforms to and implements UICollectionViewDatasource protocol methods.\n* Conforms to and implements UICollectionViewDelegate protocol methods.\n* Conforms to and implements UICollectionViewDelegateFlowLayout protocol methods.\n\n__[Working with NavigationItems](https://gist.github.com/ccabanero/4a1a4bfbf5fa3fbbb1070c8765c3de73)__\n\n* Testing initialization and state of back button (navigationItem.backBarButtonItem).\n* Testing state of custom title view (navigationItem.titleView).\n* Testing target-action pattern for Right Bar Button (navigationItem.rightBarButtonItem).\n* Testing UIBarButtonItem can pop ViewController from navigation stack.\n* [Testing UIRightBarButtonItem has correct custom UIImage when toggled](https://gist.github.com/ccabanero/ed9111e472f6cb283ff8c3e714712efa)\n\n__[Working with a ViewController composed of UISearchBar](https://gist.github.com/ccabanero/0f20b0708c0d756327995e58ff3309d4)__\n\n* Testing initialization and state of search bar after view loads.\n* Conforms to SearchBarDelegate protocol methods.\n* Testing of auto-suggest behavior between SearchBar.text and UITableView items.\n* Testing pre-processing of Search Text when 'Search' button tapped on virtual/soft keyboard.\n\n__Working with Segues between ViewControllers__\n\n* [Storyboard has target Segue](https://gist.github.com/ccabanero/a0fbb675f44a5136d2811d21a77e332a)\n* [Testing a Segue is called using a Mock UIViewController](https://gist.github.com/ccabanero/9f7ad6775eacec3cc997)\n* [Testing Segue passes data to Target ViewController](https://gist.github.com/ccabanero/308db4882b7ca4967ebb5d17530177f3)\n* [Testing ViewController can respond to an Unwind Segue](https://gist.github.com/ccabanero/177a54d2be3694f08c4f3c8f02f74394)\n\n__[Working with a ViewController composed of Custom Views](https://gist.github.com/ccabanero/ac7237e4591092130326)__\n\n* Testing initialization and state of Custom View properties when view loads.\n\n__[Working with a ViewController composed of MKMapView](https://gist.github.com/ccabanero/90c73c46ed1671298775)__\n\n* Testing state of MKMapView after view loads.\n* Conforms to and implements MKMapViewDelegate protocol methods.\n* Testing MapView adds annotations to the map.\n* Testing MapView adds specific types of MKAnnotation.\n\n__[Working with a ViewConroller that uses CoreLocation](https://gist.github.com/ccabanero/dd35e7bee8205ad225f3de1391027aa5)__\n\n* Conforms to and implements CLLocationManager protocol methods.\n\n__[Working with a ViewController composed of a UISegmentedControl](https://gist.github.com/ccabanero/e204496231a41759fa94)__\n\n* Testing initialization and state of UISegmentedControl after view loads.\n* Testing UISegmentedControl action method is invoked on change.\n\n__[Working with a ViewController composed of a UIPickerView](https://gist.github.com/ccabanero/8d1dfa65218b8bb10ebf)__\n\n* Testing initialization and state of UIPickerView after view loads.\n* Conforms to and implements UIPickerViewDataSource protocol methods.\n* Conforms to and implements UIPickerViewDelegate protocol methods.\n\n__[Working with a ViewController composed of a UIButton](https://gist.github.com/ccabanero/b92197516342c0147688)__\n\n* Testing initialization and state of UIButton after view loads.\n* Testing target-action pattern of UIButton.\n\n__[Working with a ViewController composed of a UILabel](https://gist.github.com/ccabanero/68cd8ff461316930f707)__\n\n* Testing initialization and state of UILabel after view loads.\n* Testing label state after invoking the action methods of other controls.\n\n\n__[Working with a ViewController that presents a UIAlertController](https://gist.github.com/ccabanero/b88a77caba37f8dd9fbf)__\n\n* Using a mock ViewController to confirm launching UIAlertViewController\n* Testing the initialization and state of UIAlertController once presented.\n\n### Sample Unit Tests for Model Classes\n\n[Testing Model Class Initialization](https://gist.github.com/ccabanero/90c6e2aadfd4efa6b059333edeb2b314) \n\n[Testing Model Class methods](https://gist.github.com/ccabanero/4221831a4c527c0453a8506628df34af)\n\n[Asynchronous Integration Test When Model performs work over the Network](https://gist.github.com/ccabanero/24a46c777bb29da95ba5)\n\n[Model adopts the MKAnnotation protocol](https://gist.github.com/ccabanero/f6f8aeb7ea06073753bf)\n\n[Model can Process the Response From Async Network Request](https://gist.github.com/ccabanero/1160dc6d95182593d111)\n\n[Confirming Model object instantiation of a NSManagedObject subclass](https://gist.github.com/ccabanero/93501b0cc78e2023f119)\n\n[Confirming that a NSManagedObject subclass Category properly seeds CoreData](https://gist.github.com/ccabanero/3de1a0cfecc7cb4fa9e6)\n\n### Set Up for Testing ViewController\n\n* When unit testing ViewController classes in storyboards, make sure to explicitly declare a 'Storyboard ID' property in the Identity Inspector for that ViewController.\n* Use the __@testable__ attribute before declaring your test case class (see sample below).\n* In the sample below, substitue __MyViewController__ with the name of your UIViewController you are testing.\n\n````\n    import XCTest\n    @testable import YourAppTargetName\n\n    class UnitTestsTests: XCTestCase {\n\n        var viewControllerUnderTest: MyViewController!\n\n        override func setUp() {\n            super.setUp()\n\n            let storyboard = UIStoryboard(name: \"Main\", bundle: nil)\n            self.viewControllerUnderTest = storyboard.instantiateViewController(withIdentifier: \"MyViewController\") as! MyViewController\n\n            self.viewControllerUnderTest.loadView()\n            self.viewControllerUnderTest.viewDidLoad()\n        }\n\n        override func tearDown() {\n            // Put teardown code here. This method is called after the invocation of each test method in the class.\n            super.tearDown()\n        }\n\n        func testStuff() {\n           XCTAssertEqual(1, 1)\n        }\n    }\n```` \n\n### Add a Test Case file\n\nBelow are steps for adding a Test Case file for an individual class that you are testing.\n\n* Right-click your Tests folder in the Xcode Project Navigator\n* Choose New File\n* Choose Cocoa Touch Class\n* Use a naming convention - such as appending Test to your class name.  For example, use MapViewControllerTest for testing MapViewController.\n* Choose the Subclass of __XCTestCase__\n* Confirm the Test target is selected\n* Choose Create\n\n### Connect\n\n* Twitter: [@clintcabanero](http://twitter.com/clintcabanero)\n* GitHub: [ccabanero](http:///github.com/ccabanero)\n\n\n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccabanero%2Fios-unit-testing-patterns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fccabanero%2Fios-unit-testing-patterns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccabanero%2Fios-unit-testing-patterns/lists"}