Swift: check if Segue exists, has the correct destination and changes the correct settings

I am new to quick testing, and for now I want to try four tests. I have a segue from ViewController to another (searchForViewController). For this segue, I want to check this 4 things:

  • Is there a segue with the id "searchSegue"?
  • Is a call to segue called, then I click the "Search by button" button?
  • Is the purpose of the Segue SearchedForViewController
  • Is Segue replacing the SearchedForVC passed line variable with garten?

My segue in Main.storyboard looks like this:

Segue

My code for segue is similar to the following (it works, but for the future I just want to know how to test):

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? 
    }
}

EDIT , segueShouldExist. . :

func getSegues(ofViewController viewController: UIViewController) -> [String] {
        let identifiers = (viewController.value(forKey: "storyboardSegueTemplates") as? [AnyObject])?.flatMap({ $0.value(forKey: "identifier") as? String }) ?? []
        return identifiers
    }
+4
1

, .

# 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 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
    }
}

prepare(for:,sender:) ViewController :

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"
    }
}

, .

0

Source: https://habr.com/ru/post/1668615/


All Articles