Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Unit Testing UIViewController
Search
Parveen Kaler
September 14, 2018
Programming
0
360
Unit Testing UIViewController
Lightning talk from NSSpain 2018.
5 tips in 5 minutes.
Parveen Kaler
September 14, 2018
Tweet
Share
More Decks by Parveen Kaler
See All by Parveen Kaler
Millions of Customers. Billions of Dollars: iOS Architecture at Scale
kaler
0
2.4k
What's New in Swift 2.0
kaler
0
760
Opinionated Core Data: Hold On To Your Butts
kaler
0
1.6k
Other Decks in Programming
See All in Programming
CEDEC 2025 『ゲームにおけるリアルタイム通信への QUIC導入事例の紹介』
segadevtech
3
970
WebAssemblyインタプリタを書く ~Component Modelを添えて~
ruccho
1
910
SOCI Index Manifest v2が出たので調べてみた / Introduction to SOCI Index Manifest v2
tkikuc
1
110
物語を動かす行動"量" #エンジニアニメ
konifar
14
5.4k
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
190
kiroでゲームを作ってみた
iriikeita
0
180
Claude Codeで実装以外の開発フロー、どこまで自動化できるか?失敗と成功
ndadayo
2
1.3k
Introduction to Git & GitHub
latte72
0
120
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
380
AHC051解法紹介
eijirou
0
620
CSC305 Summer Lecture 06
javiergs
PRO
0
100
デザインシステムが必須の時代に
yosuke_furukawa
PRO
2
110
Featured
See All Featured
Designing for Performance
lara
610
69k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Optimizing for Happiness
mojombo
379
70k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
A Tale of Four Properties
chriscoyier
160
23k
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Transcript
UNIT TESTING UIVIEWCONTROLLERS 5 TIPS IN 5 MINUTES !!!!! 1
— Parveen Kaler • @kaler • https://parveenkaler.com
! TIP #1 2 — Parveen Kaler • @kaler •
https://parveenkaler.com
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
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
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 — Parveen Kaler • @kaler • https://parveenkaler.com
7 — Parveen Kaler • @kaler • https://parveenkaler.com
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
TIP #2 9 — Parveen Kaler • @kaler • https://parveenkaler.com
10 — Parveen Kaler • @kaler • https://parveenkaler.com
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
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
TIP #3 13 — Parveen Kaler • @kaler • https://parveenkaler.com
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
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
TIP #4 16 — Parveen Kaler • @kaler • https://parveenkaler.com
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
TIP #5 18 — Parveen Kaler • @kaler • https://parveenkaler.com
19 — Parveen Kaler • @kaler • https://parveenkaler.com
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
// ! 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
var sut: ViewController! func testViewContainment() { XCTAssertNotNil(sut.topViewController) // SUCCESS XCTAssertNotNil(sut.bottomViewController)
// SUCCESS } 22 — Parveen Kaler • @kaler • https://parveenkaler.com
UNIT TESTING UIVIEWCONTROLLERS ! 5 TIPS IN 5 MINUTES 23
— Parveen Kaler • @kaler • https://parveenkaler.com