Last active
November 9, 2023 09:00
-
-
Save ccabanero/39ee6d5fd7b289dee695 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with a ViewController composed of TableViews
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
import XCTest | |
@testable import YourAppTargetname | |
class SideMenuViewControllerTest: XCTestCase { | |
var viewControllerUnderTest: SideMenuViewController! | |
override func setUp() { | |
super.setUp() | |
let storyboard = UIStoryboard(name: "Main", bundle: nil) | |
self.viewControllerUnderTest = storyboard.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController | |
// in view controller, menuItems = ["one", "two", "three"] | |
self.viewControllerUnderTest.loadView() | |
self.viewControllerUnderTest.viewDidLoad() | |
} | |
override func tearDown() { | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
super.tearDown() | |
} | |
func testHasATableView() { | |
XCTAssertNotNil(viewControllerUnderTest.tableView) | |
} | |
func testTableViewHasDelegate() { | |
XCTAssertNotNil(viewControllerUnderTest.tableView.delegate) | |
} | |
func testTableViewConfromsToTableViewDelegateProtocol() { | |
XCTAssertTrue(viewControllerUnderTest.conforms(to: UITableViewDelegate.self)) | |
XCTAssertTrue(viewControllerUnderTest.responds(to: #selector(viewControllerUnderTest.tableView(_:didSelectRowAt:)))) | |
} | |
func testTableViewHasDataSource() { | |
XCTAssertNotNil(viewControllerUnderTest.tableView.dataSource) | |
} | |
func testTableViewConformsToTableViewDataSourceProtocol() { | |
XCTAssertTrue(viewControllerUnderTest.conforms(to: UITableViewDataSource.self)) | |
XCTAssertTrue(viewControllerUnderTest.responds(to: #selector(viewControllerUnderTest.numberOfSections(in:)))) | |
XCTAssertTrue(viewControllerUnderTest.responds(to: #selector(viewControllerUnderTest.tableView(_:numberOfRowsInSection:)))) | |
XCTAssertTrue(viewControllerUnderTest.responds(to: #selector(viewControllerUnderTest.tableView(_:cellForRowAt:)))) | |
} | |
func testTableViewCellHasReuseIdentifier() { | |
let cell = viewControllerUnderTest.tableView(viewControllerUnderTest.tableView, cellForRowAt: IndexPath(row: 0, section: 0)) as? SideMenuTableViewCell | |
let actualReuseIdentifer = cell?.reuseIdentifier | |
let expectedReuseIdentifier = "SideMenuTableViewCell" | |
XCTAssertEqual(actualReuseIdentifer, expectedReuseIdentifier) | |
} | |
func testTableCellHasCorrectLabelText() { | |
let cell0 = viewControllerUnderTest.tableView(viewControllerUnderTest.tableView, cellForRowAt: IndexPath(row: 0, section: 0)) as? SideMenuTableViewCell | |
XCTAssertEqual(cell0?.label.text, "one") | |
let cell1 = viewControllerUnderTest.tableView(viewControllerUnderTest.tableView, cellForRowAt: IndexPath(row: 1, section: 0)) as? SideMenuTableViewCell | |
XCTAssertEqual(cell1?.label.text, "two") | |
let cell2 = viewControllerUnderTest.tableView(viewControllerUnderTest.tableView, cellForRowAt: IndexPath(row: 2, section: 0)) as? SideMenuTableViewCell | |
XCTAssertEqual(cell2?.label.text, "three") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can we test if data fetch from server?