swift - 在 xib 文件中对自定义 tableviewcell 进行单元测试
问题描述
我在情节提要中有一个带有 tableview 的视图控制器类。在另一个单独的 xib 文件中,我有一个自定义表格视图单元格,其中包含自定义表格视图单元格类中的图像视图、标签等属性。
现在我必须编写单元测试用例来测试这个自定义单元格及其属性。我尝试了以下方法,
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”。如何测试我的自定义表格视图单元格及其属性?提前致谢。
解决方案
首次使用:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let yourView = storyboard.instantiateViewController(withIdentifier: "yourView") as! yourView
然后打电话
let customCell = yourView.myTableView.dequeueReusableCell(withIdentifier: "yourView") as! NewsDetailsTableViewCell
推荐阅读
- python - boto 3 中 Comprehend 的“DataAccessRoleArn”是什么?
- dynamics-crm - Dynamics 365 FetchXML - 对字符串使用 IN 运算符
- identityserver4 - Identity Server 4 是否符合 NIST 标准?
- rest - 如何在 REST::Client perl 中进行不安全的 SSL 连接
- javascript - 如何使用 pubnub 允许用户创建群聊
- python - 接收图像帧并将数据转换为图像文件并使用烧瓶将其保存在文件夹中
- arrays - Ada 中堆栈分配数组的访问类型
- instagram - 如何在 Spark AR 中为所有设备设置相同的 2D 文本位置
- kubernetes - Kubernetes 仪表板无法访问
- java - 无法在 Android Studio 中使用 CoordinatorLayout