XIB文件中的单元测试自定义TableViewCell

时间:2018-06-27 09:06:32

标签: swift unit-testing uitableview

我在Storyboard中有一个带有tableview的视图控制器类。在另一个单独的xib文件中,我有一个自定义表格视图单元格,其中包含一些属性,例如自定义表格视图单元格类中的图像视图,标签..etc。

现在,我必须编写单元测试用例来测试此自定义单元及其属性。我尝试了以下方法,

import XCTest
@testable import Citizens_Bank

class InvestmentTableViewCellTests: XCTestCase {

    var newsVC: NewsViewController!

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the  invocation of each test method in the class.

    }

   func testCustomViewContainsAView() {
    let bundle = Bundle(for: NewsDetailsTableViewCell.self)
    guard let _ = bundle.loadNibNamed("NewsDetailsTableViewCell", owner: nil)?.first as? UIView else {
        return XCTFail("CustomView nib did not contain a UIView")
     }
  }

  func testCustomCell() {
     let customCell: NewsDetailsTableViewCell = accountsVC.myTableView.dequeueReusableCell(withIdentifier: "newsDetailsCell") as! NewsDetailsTableViewCell
    XCTAssertNotNil(customCell, "No Custom Cell Available")
  }
}

testCustomcell测试用例功能因控制台日志中的此错误而崩溃“严重错误:在展开可选值时意外发现nil”。如何测试自定义表格视图单元格及其属性?提前致谢。

1 个答案:

答案 0 :(得分:0)

首次使用:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let yourView = storyboard.instantiateViewController(withIdentifier: "yourView") as! yourView

然后致电

 let customCell = yourView.myTableView.dequeueReusableCell(withIdentifier: "yourView") as! NewsDetailsTableViewCell
相关问题