Skip to content

Instantly share code, notes, and snippets.

  • Save kjoe07/04ffff2a25b429cd4acbb767de49f0ff to your computer and use it in GitHub Desktop.
Save kjoe07/04ffff2a25b429cd4acbb767de49f0ff to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Working with a ViewController composed of TableViews
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