Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing UIViewController

Parveen Kaler
September 14, 2018

Unit Testing UIViewController

Lightning talk from NSSpain 2018.
5 tips in 5 minutes.

Parveen Kaler

September 14, 2018
Tweet

More Decks by Parveen Kaler

Other Decks in Programming

Transcript

  1. UNIT TESTING UIVIEWCONTROLLERS 5 TIPS IN 5 MINUTES !!!!! 1

    — Parveen Kaler • @kaler • https://parveenkaler.com
  2. class FirstTests: XCTestCase { var sut: ViewController! override func setUp()

    { super.setUp() let sut = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! ViewController } } 3 — Parveen Kaler • @kaler • https://parveenkaler.com
  3. var sut: ViewController! override func setUp() { super.setUp() let sut

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! ViewController } func testViewNotNil() { XCTAssertNotNil(sut.view) // ❎ FAIL } 4 — Parveen Kaler • @kaler • https://parveenkaler.com
  4. var sut: ViewController! override func setUp() { super.setUp() let sut

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! ViewController sut.loadView() // ! DON'T!!! } func testViewNotNil() { XCTAssertNotNil(sut.view) // ❎ FAIL } 5 — Parveen Kaler • @kaler • https://parveenkaler.com
  5. var sut: ViewController! override func setUp() { super.setUp() let sut

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! ViewController UIApplication.shared.keyWindow?.rootViewController = sut // Run View Lifecycle } func testViewNotNil() { XCTAssertNotNil(sut.view) // ✅ SUCCESS } 8 — Parveen Kaler • @kaler • https://parveenkaler.com
  6. var sut: ViewController! override func setUp() { super.setUp() let tab

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! UITabBarController UIApplication.shared.keyWindow?.rootViewController = tab // Run View Lifecycle let nav = tab.selectedViewController as! UINavigationController sut = nav.topViewController as! ViewController UIApplication.shared.keyWindow?.rootViewController = sut // Run View Lifecycle } 11 — Parveen Kaler • @kaler • https://parveenkaler.com
  7. var sut: ViewController! override func setUp() { super.setUp() let tab

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! UITabBarController UIApplication.shared.keyWindow?.rootViewController = tab // Run View Lifecycle let nav = tab.selectedViewController as! UINavigationController sut = nav.topViewController as! ViewController UIApplication.shared.keyWindow?.rootViewController = sut // Run View Lifecycle } func testViewNotNil() { XCTAssertNotNil(sut.view) // ✅ SUCCESS } 12 — Parveen Kaler • @kaler • https://parveenkaler.com
  8. var sut: SecondViewController! override func setUp() { super.setUp() let tab

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! UITabBarController UIApplication.shared.keyWindow?.rootViewController = tab // Run View Lifecycle tab.selectedIndex = 1 // ! Switch tabs let nav = tab.selectedViewController as! UINavigationController sut = nav.topViewController as! SecondViewController UIApplication.shared.keyWindow?.rootViewController = sut // Run View Lifecycle } 14 — Parveen Kaler • @kaler • https://parveenkaler.com
  9. var sut: SecondViewController! override func setUp() { super.setUp() let tab

    = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! UITabBarController UIApplication.shared.keyWindow?.rootViewController = tab // Run View Lifecycle tab.selectedIndex = 1 // ! Switch tabs let nav = tab.selectedViewController as! UINavigationController sut = nav.topViewController as! SecondViewController UIApplication.shared.keyWindow?.rootViewController = sut // Run View Lifecycle } func testViewNotNil() { XCTAssertNotNil(sut.view) // ✅ SUCCESS } 15 — Parveen Kaler • @kaler • https://parveenkaler.com
  10. var sut: SecondViewController! override func setUp() { super.setUp() sut =

    UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController UIApplication.shared.keyWindow?.rootViewController = sut // ! Run View Lifecycle } 17 — Parveen Kaler • @kaler • https://parveenkaler.com
  11. class ViewController: UIViewController { var topViewController: TopViewController! var bottomViewController: BottomViewController!

    // ! prepare() passes VCs in segue override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? TopViewController { topViewController = vc } else if let vc = segue.destination as? BottomViewController { bottomViewController = vc } } } 20 — Parveen Kaler • @kaler • https://parveenkaler.com
  12. // ! prepare() passes VCs in segue override func prepare(for

    segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? TopViewController { topViewController = vc } else if let vc = segue.destination as? BottomViewController { bottomViewController = vc } } // ! prepare() is called before viewDidLoad() override func viewDidLoad() { super.viewDidLoad() } 21 — Parveen Kaler • @kaler • https://parveenkaler.com
  13. UNIT TESTING UIVIEWCONTROLLERS ! 5 TIPS IN 5 MINUTES 23

    — Parveen Kaler • @kaler • https://parveenkaler.com