我是快速测试的新手,目前我想尝试四项测试.我有一个从ViewController到另一个的segue(searchingForViewController).对于这个segue,我想测试这4件事: 是否存在标识符为“searchSegue”的segue? 是否会
>是否存在标识符为“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({ $0.value(forKey: "identifier") as? String }) ?? [] return identifiers }要实现这一点,您需要重新调整代码.
# ViewControllerTests.swift var 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, 1, "Segue count should equal one.") XCTAssertTrue(identifiers.contains("searchSegue"), "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({ $0.value(forKey: "identifier") as? String }) ?? [] return identifiers } }
这项工作的原因是改变了ViewController中的prepare(for:,sender :)方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "searchSegue" { guard let controller = segue.destination as? SearchedForViewController else { return } controller.passedString = "garten" } }
至:
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" } }
希望有所帮助.