swift - Tests for custom UITableViewCell, cellForRowAtIndexPath crashes with nil outlets -
i have viewcontroller contains tableview. since need keep code covered tests, need write test [tableview:cellforrowatindexpath]
import uikit class mainviewcontroller: uiviewcontroller, uitableviewdatasource { @iboutlet weak var tableview: uitableview! var source = [ country(name: "usa", capital: "washington, d.c."), country(name: "argentina", capital: "buenos aires"), country(name: "mexico", capital: "mexico, d.f.") ] let cellidentifier = nsstringfromclass(mainviewcontroller.self) init() { super.init(nibname: "mainviewcontroller", bundle: nil) } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } override func viewdidload() { super.viewdidload() let nib = uinib(nibname: "customcell", bundle: nil) tableview?.registernib(nib, forcellreuseidentifier: cellidentifier) tableview.datasource = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return source.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier(cellidentifier) as? customcell if let cell = cell { cell.countrylabel.text = source[indexpath.row].name cell.capitallabel.text = source[indexpath.row].capital return cell } return customcell() } } struct country { var name: string var capital: string }
the tests following:
import uikit import xctest class mainviewcontrollertests: xctestcase { var controller: mainviewcontroller! override func setup() { super.setup() controller = mainviewcontroller() controller.view.description } override func teardown() { super.teardown() } func testtableviewoutlet() { xctassertnotnil(controller.tableview) } func testtableviewcellforrowatindexpath() { let indexpath = nsindexpath(forrow: 0, insection: 0) let cell = controller.tableview(controller.tableview, cellforrowatindexpath: indexpath) as! customcell xctassertequal(cell.countrylabel.text!, "usa") xctassertequal(cell.capitallabel.text!, "washington, d.c.") } }
i following error when running tests:
test case '-[customtableviewcelltests.mainviewcontrollertests testtableviewcellforrowatindexpath]' started. fatal error: unexpectedly found nil while unwrapping optional value
this function has alluded tests months in production application since [dequeuereusablecellwithidentifier] returns nil when run tests. have tried many different approaches have read in stackoverflow, like:
- creating customcell when [dequeuereusablecellwithidentifier] returns nil
- loading nib customcell using temporarycontroller , grabbing view, supposed connected cell
the bottom line close solution, create customcell object, outlets cell remain nil. haven't found piece of code realize outlets initialized elements can make tests pass.
i including basic ios project own git contains code have included here. uiviewcontroller contains uitableview displays data using custom uitableviewcell. i'm hoping find solution realizes custom cell outlets make test pass.
git repo source: https://bitbucket.org/jallauca/customcell/src
to clone git repo:
git clone https://bitbucket.org/jallauca/customcell.git
for me, (swift 3) had call cellforrow datasource cellforrowat function instead of accessing straight tableview.cellforrow function
//this let cell = controller.tableview(controller.tableview, cellforrowat: indexpath(row: 0, section: 0)) //instead of let cell = controller.tableview.cellforrow(at: indexpath(row: 0, section: 0))
Comments
Post a Comment