我有一个 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/