The following line crashes the program:
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
The console log shows the following error:
"caught "NSInternalInconsistencyException", "request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})"
Here is a bullet point list of things I've checked:
Here is the full code:
import XCTest
@testable import ToDo
class ItemCellTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSUT_HasNameLabel(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ItemListViewController") as! ItemListViewController
_=controller.view
let tableView = controller.tableView
tableView.dataSource = FakeDataSource()
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
XCTAssertNotNil(cell.titleLabel)
}
}
extension ItemCellTests{
class FakeDataSource: NSObject, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
}
Here is the ItemCell.swift code:
import UIKit
class ItemCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
func configCellWithItem(item: ToDoItem){
}
}
All properties that are tied to the storyboard are connected. What is being overlooked?
Looks like this code is from my book. That's a known error in the book. Change the test method to the following:
func testSUT_HasNameLabel(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ItemListViewController") as! ItemListViewController
_=controller.view
let tableView = controller.tableView
let dataProvider = FakeDataSource()
tableView.dataSource = dataProvider
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! ItemCell
XCTAssertNotNil(cell.titleLabel)
}
Then, when you later refactor that code to put the setup into the setUp
method, add a property let dataProvider = FakeDataSource()
to the test case and set it to the data source of the table view (tableView.dataSource = dataProvider
) in setUp
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With