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
340
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
720
Opinionated Core Data: Hold On To Your Butts
kaler
0
1.4k
Other Decks in Programming
See All in Programming
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Quine, Polyglot, 良いコード
qnighy
4
640
CSC509 Lecture 11
javiergs
PRO
0
180
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
受け取る人から提供する人になるということ
little_rubyist
0
230
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
200
Jakarta EE meets AI
ivargrimstad
0
600
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
subpath importsで始めるモック生活
10tera
0
300
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
160
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
136
6.6k
Thoughts on Productivity
jonyablonski
67
4.3k
Adopting Sorbet at Scale
ufuk
73
9.1k
Rails Girls Zürich Keynote
gr2m
94
13k
Building Your Own Lightsaber
phodgson
103
6.1k
Faster Mobile Websites
deanohume
305
30k
Building Applications with DynamoDB
mza
90
6.1k
Six Lessons from altMBA
skipperchong
27
3.5k
For a Future-Friendly Web
brad_frost
175
9.4k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
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