我正在用Swift 2構建iOS應用程序,該應用程序使用自定義表格視圖單元格以及附加標籤,圖像視圖等(我們稱之爲類CustomTableViewCell
)。我爲每個子視圖創建了類別故事板連接併爲該單元分配了一個標識符。我嘲笑了數據並試圖運行應用程序來檢查單元格是否正確映射,並且看起來沒問題。測試自定義TableViewCell的內容
問題是,我無法將出隊單元視爲CustomTableViewCell
來測試其屬性的值。當我downcast從tableView(tableView, cellForRowAtIndexPath: indexPath)
返回的單元格時,所有自定義屬性值都變爲nil
,我的測試失敗。
這裏是我的代碼:
MyViewControllerTests.swift
func testShouldConfigureTableViewCellToDisplayNotification() {
// Given
sut.tableView = TableViewSpy()
let items = [ <some items to display> ]
sut.displayedItems = items
// When
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = viewController.tableView(viewController.tableView, cellForRowAtIndexPath: indexPath) as! CustomTableViewCell
// Then
XCTAssertEqual(cell.detailLabel?.text, "foo", "A properly configured table view cell should display the notification detail")
XCTAssertEqual(cell.titleLabel?.text, "Bar", "A properly configured table view cell should display the notification title")
XCTAssertEqual(cell.dateLabel?.text, "15/04/2016", "A properly configured table view cell should display the notification date")
}
MyViewController.swift
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "CustomTableViewCell"
let displayedItem = displayedItems[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomTableViewCell
if cell == nil {
cell = CustomTableViewCell(style: .Value1, reuseIdentifier: cellIdentifier)
}
cell!.dateLabel?.text = displayedItem.date
cell!.detailLabel?.text = displayedItem.detail
cell!.titleLabel?.text = displayedItem.title
return cell!
}
CustomTableViewCell.swift
class CustomTableViewCell: UITableViewCell {
// MARK: Properties
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
這使得Xcode中拋出一些構建錯誤:'不能強迫非可選類型「CustomTableViewCell」'的展開價值,因爲我把它拆開當細胞被出隊。當我從屬性綁定中刪除所有這些解包時,運行測試時出現致命錯誤:''意外地發現零,而解包可選值「' – lerp90
我的壞!檢查更新答案。問題在於使用「cell!」在出現細胞後:) –
這意味着你沒有在故事板中正確指定你的細胞。我現在將更新我的答案,提供您需要檢查的其他內容 –