swift - xib 文件中的单元测试自定义 tableviewcell

标签 swift unit-testing uitableview

我有一个 View Controller 类,在 Storyboard中有一个表格 View 。在另一个单独的 xib 文件中,我有一个自定义表格 View 单元格,其中包含一些属性,例如自定义表格 View 单元格类中的 ImageView 、标签等。

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

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 测试用例函数因控制台日志中的此错误而崩溃“ fatal error :展开可选值时意外发现 nil”。如何测试我的自定义表格 View 单元格及其属性?提前致谢。

最佳答案

首次使用:

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

然后调用

 let customCell = yourView.myTableView.dequeueReusableCell(withIdentifier: "yourView") as! NewsDetailsTableViewCell

关于swift - xib 文件中的单元测试自定义 tableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51058718/

相关文章:

iphone - 注册Nib :forReuseidentifier with custom UTTableViewCell and Storyboards

angularjs - Angular Karma 单元测试 : How to inject a mock service in karma configuration instead of in all test specs

c++ - 如何跳过 BOOST 单元测试?

ios - 将 UITableView Cell 滚动到表格边界之外

ios - 如何计算对象数组中 keyValue 的总数

swift - 如何在 swift3.x 中使用 AnyClass 初始化特定类实例

ios - 将数据写入 firebase 而不覆盖它。 swift

iOS Swift3 TextView 行在错误的位置

ruby-on-rails - 运行 rails rake : test in Parallel (on multiple cores/speeding it up) using Miniest

iphone - 如何在 UITableViewCell 中获取 UISwitch 两侧都有文本?