我想将Show
segue附加到storyboard中的表视图的属性单元格中。现在我想测试这个绑定是否存在。即如果我在Interface Builder中删除segue或更改了segue标识符,我想要进行测试。
从用户的角度来看,用户点击表格视图的单元格,然后应该执行此segue。
从测试角度来看,我可以调整prepare(for:sender:)
方法来验证segue的执行情况,但我不知道如何触发"点按"编程。
我已经尝试了tableView.selectRow(at:animated:scrollPosition)
,cell.setSelected(_:animated:)
,但这些都没有效果。
我知道如何以编程方式触发segue - 因此这不是这里要求的。
segue触发工作由故事板在场景后面完成,生产代码中没有segue触发代码(只有覆盖prepare(for:sender:)
)。该应用程序按预期工作。这里的问题是我需要一个测试来保证这种幕后触发始终存在,即:如果有一天我喝醉了并且错误地编辑了故事板以将原始的segue连接到不相关的地方,那么就会有一个测试去红了,踢我的屁股。
答案 0 :(得分:0)
我能够测试连接到视图控制器而不是表视图单元格的segue。
在应用程序中,我在点击单元格时执行segue。这大致相当于将segue连接到表格视图单元格所得到的结果。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showNextView", sender: nil)
}
这允许对segue进行如下测试:
func testSegue() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
// create the view controller that has the segue to be tested
let viewController = appDelegate.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
// assuming the view controller does a Show segue, put it in a navigation controller
let navigationController = UINavigationController(rootViewController: viewController)
// this is needed or test fails!
navigationController.view.layoutIfNeeded()
// replace the root view controller with the navigation controller
appDelegate.window!.rootViewController = navigationController
// finally, select the row! this fires the segue
viewController.tableView(viewController.tableView, didSelectRowAt: IndexPath(row: 0, section: 0))
// assert something about the result of the segue
XCTAssertTrue(navigationController.visibleViewController is SeguedViewController)
}
答案 1 :(得分:-1)
只需使用此功能:
[self performSegueWithIdentifier:@"YOURIDENTIFIER" sender:self];
它可以帮助您测试您的segue。