Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caught NSInternalInconsistencyException request for rect at invalid indexPath

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:

  • Correct Storyboard ID is listed in the identity inspector
  • Correct custom class is listed in the identity inspector
  • Attributes inspector has "ItemCell" in the Table View Cell identifier field

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?

like image 736
Laurence Wingo Avatar asked Aug 02 '16 11:08

Laurence Wingo


1 Answers

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.

like image 160
dasdom Avatar answered Sep 21 '22 02:09

dasdom