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
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
270
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
Haze - Real time background blurring
chrisbanes
1
510
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
430
Zoneless Testing
rainerhahnekamp
0
120
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
useSyncExternalStoreを使いまくる
ssssota
6
1.1k
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
たのしいparse.y
ydah
3
120
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Optimizing for Happiness
mojombo
376
70k
The Invisible Side of Design
smashingmag
298
50k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Faster Mobile Websites
deanohume
305
30k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
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