>是否存在标识符为“searchSegue”的segue?
>是否会调用segue然后点击按钮搜索?
> SEOgle的目的地是SearchedForVIEwController
> Segue将SearchedForVC的变量“passstring”更改为“garten”吗?
我在Main.storyboard中的segue如下所示:
我的segue代码如下(工作,但未来我只想知道如何测试):
overrIDe func prepare(for segue: UIStoryboardSegue,sender: Any?) { if segue.IDentifIEr == "searchSegue" { guard let controller = segue.destination as? SearchedForVIEwController else { return } controller.passedString = "garten" } }
目前我的测试类就像是空的,看起来像这样(只是加载视图):
class VIEwControllerTests: XCTestCase { var vc: VIEwController! overrIDe func setUp() { super.setUp() let storyboard = UIStoryboard(name: "Main",bundle: Bundle.main) vc = storyboard.instantiateVIEwController(withIDentifIEr: "test") as! VIEwController let _ = vc.vIEw } func testSegueShouldExist() { let IDentifIErs = getSegues(ofVIEwController: vc) XCTAssertEqual(IDentifIErs.count,1,"Segue count should equal one.") XCTAssertTrue(IDentifIErs.contains("searchSegue"),"Segue searchSegue should exist.") } func testSegueActionsThenPushSearchbutton() { // code here } func testSegueDirectsToSearchedForVIEwController() { // code here } func testSeguePassedGartenAsValuetoSearchedForVC() { // call prepare and test after? }}
编辑我发现了如何实现segueShouldExist.我添加了代码.它调用辅助函数:
func getSegues(ofVIEwController vIEwController: UIVIEwController) -> [String] { let IDentifIErs = (vIEwController.value(forKey: "storyboardSegueTemplates") as? [AnyObject])?.flatMap({解决方法 要实现这一点,您需要重新调整代码.# VIEwControllerTests.swiftvar capturedSegue: UIStoryboardSegue?extension VIEwController { overrIDe open func prepare(for segue: UIStoryboardSegue,sender: Any?) { prepareSearchForVC(with: segue) capturedSegue = segue }}class VIEwControllerTests: XCTestCase { var vc: VIEwController! overrIDe func setUp() { super.setUp() let storyboard = UIStoryboard(name: "Main",bundle: Bundle.main) vc = storyboard.instantiateInitialVIEwController() as! VIEwController vc.loadVIEwIfNeeded() } func testSegueShouldExist() { let IDentifIErs = getSegues(ofVIEwController: vc) XCTAssertEqual(IDentifIErs.count,"Segue searchSegue should exist.") } func testSegueActionsThenPushSearchbutton() { // Can programmatically send actions to UIControl classes vc.searchbutton.sendActions(for: .touchUpInsIDe) // Just makes sure any segue was called // since there is only one in this case there's no need to be more specific XCTAssertNotNil(capturedSegue) } func testSegueDirectsToSearchedForVIEwController() { vc.performSegue(withIDentifIEr: "searchSegue",sender: nil) // Attempts to cast the captured segue let destination = capturedSegue?.destination as? SearchedForVC XCTAssertNotNil(destination,"Destination should be searched for controller") } func testSeguePassedGartenAsValuetoSearchedForVC() { vc.performSegue(withIDentifIEr: "searchSegue",sender: nil) let destination = capturedSegue?.destination as? SearchedForVC XCTAssertEqual(destination?.passedString,"garten") } func getSegues(ofVIEwController vIEwController: UIVIEwController) -> [String] { let IDentifIErs = (vIEwController.value(forKey: "storyboardSegueTemplates") as? [AnyObject])?.flatMap({.value(forKey: "IDentifIEr") as? String }) ?? [] return IDentifIErs }overrIDe func prepare(for segue: UIStoryboardSegue,sender: Any?) { if segue.IDentifIEr == "searchSegue" { guard let controller = segue.destination as? SearchedForVIEwController else { return } controller.passedString = "garten" }}.value(forKey: "IDentifIEr") as? String }) ?? [] return IDentifIErs }}
overrIDe func prepare(for segue: UIStoryboardSegue,sender: Any?) { prepareSearchForVC(with: segue)}func prepareSearchForVC(with segue: UIStoryboardSegue) { if segue.IDentifIEr == "searchSegue" { guard let controller = segue.destination as? SearchedForVC else { return } controller.passedString = "garten" }}
这项工作的原因是改变了VIEwController中的prepare(for:,sender :)方法:
至:
希望有所帮助.
总结以上是内存溢出为你收集整理的Swift:测试Segue是否存在,有正确的目的地并改变正确的东西全部内容,希望文章能够帮你解决Swift:测试Segue是否存在,有正确的目的地并改变正确的东西所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)