Unit Testing UIViewController

9a2349983b8d0e1cd2ca8132daa3685a?s=47 Parveen Kaler
September 14, 2018

Unit Testing UIViewController

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

9a2349983b8d0e1cd2ca8132daa3685a?s=128

Parveen Kaler

September 14, 2018
Tweet

Transcript

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

    — Parveen Kaler • @kaler • https://parveenkaler.com
  2. ! TIP #1 2 — Parveen Kaler • @kaler •

    https://parveenkaler.com
  3. 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
  4. 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
  5. 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
  6. 6 — Parveen Kaler • @kaler • https://parveenkaler.com

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

  8. 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
  9. TIP #2 9 — Parveen Kaler • @kaler • https://parveenkaler.com

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

  11. 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
  12. 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
  13. TIP #3 13 — Parveen Kaler • @kaler • https://parveenkaler.com

  14. 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
  15. 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
  16. TIP #4 16 — Parveen Kaler • @kaler • https://parveenkaler.com

  17. 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
  18. TIP #5 18 — Parveen Kaler • @kaler • https://parveenkaler.com

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

  20. 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
  21. // ! 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
  22. var sut: ViewController! func testViewContainment() { XCTAssertNotNil(sut.topViewController) // SUCCESS XCTAssertNotNil(sut.bottomViewController)

    // SUCCESS } 22 — Parveen Kaler • @kaler • https://parveenkaler.com
  23. UNIT TESTING UIVIEWCONTROLLERS ! 5 TIPS IN 5 MINUTES 23

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