单元测试时获取 UITableView 中的行数 - swift?
Get number rows in UITableView while unit tests- swift?
我正在为具有 UITableView 的 UIViewController
编写测试用例。我想问一下如何获取 UITableView
中的行数
func testloadingDataIntoUiTableView()
{
var countRow:Int = viewController.formListTableView.numberOfRowsInSection
XCTAssert(countRow == 4)
}
简介
请记住,数据模型会生成 UI。但是你不应该查询 UI 来检索你的数据模型(除非我们正在谈论用户输入)。
我们来看这个例子
class Controller:UITableViewController {
let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"]
let places = ["Maveriks", "Yosemite", "El Capitan"];
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return animals.count
case 1: return places.count
default: fatalError()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID") else { fatalError("Who took the MyCellID cell???") }
switch indexPath.section {
case 0: cell.textLabel?.text = animals[indexPath.row]
case 1: cell.textLabel?.text = places[indexPath.row]
default: fatalError()
}
return cell
}
}
丑陋的解决方案
在这种情况下,要获取 table 中的总行数,我们应该查询模型(animals
和 places
属性),因此
let controller: Controller = ...
let rows = controller.animals.count + controller.places.count
很好的解决方案
或者更好的是,我们可以将 animals
和 places
属性设为私有并添加一个计算 属性 像这样
class Controller:UITableViewController {
private let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"]
private let places = ["Maveriks", "Yosemite", "El Capitan"];
var totalNumberOfRows: Int { return animals.count + places.count }
...
现在你可以使用这个了
let controller: Controller = ...
let rows = controller.totalNumberOfRows
我正在为具有 UITableView 的 UIViewController
编写测试用例。我想问一下如何获取 UITableView
func testloadingDataIntoUiTableView()
{
var countRow:Int = viewController.formListTableView.numberOfRowsInSection
XCTAssert(countRow == 4)
}
简介
请记住,数据模型会生成 UI。但是你不应该查询 UI 来检索你的数据模型(除非我们正在谈论用户输入)。
我们来看这个例子
class Controller:UITableViewController {
let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"]
let places = ["Maveriks", "Yosemite", "El Capitan"];
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return animals.count
case 1: return places.count
default: fatalError()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID") else { fatalError("Who took the MyCellID cell???") }
switch indexPath.section {
case 0: cell.textLabel?.text = animals[indexPath.row]
case 1: cell.textLabel?.text = places[indexPath.row]
default: fatalError()
}
return cell
}
}
丑陋的解决方案
在这种情况下,要获取 table 中的总行数,我们应该查询模型(animals
和 places
属性),因此
let controller: Controller = ...
let rows = controller.animals.count + controller.places.count
很好的解决方案
或者更好的是,我们可以将 animals
和 places
属性设为私有并添加一个计算 属性 像这样
class Controller:UITableViewController {
private let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"]
private let places = ["Maveriks", "Yosemite", "El Capitan"];
var totalNumberOfRows: Int { return animals.count + places.count }
...
现在你可以使用这个了
let controller: Controller = ...
let rows = controller.totalNumberOfRows