Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
How to Mock Protocols in Swift
Search
Sho Ikeda
April 19, 2019
Programming
3.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
How to Mock Protocols in Swift
「Mobile Act KYOTO #1」の発表資料です #MobileAct
https://mobileact.connpass.com/event/124732/
Sho Ikeda
April 19, 2019
More Decks by Sho Ikeda
See All by Sho Ikeda
大規模アプリにおけるXcode Previews実用化までの道のり
ikesyo
0
31k
Package Traits
ikesyo
2
1.7k
Open Source Swift Workshop - Foundation and first party libraries
ikesyo
0
3.4k
Renovateで実現するライブラリの自動更新生活 / Automated Library Updates with Renovate
ikesyo
3
740
XcodeのDevelopment Assets探訪
ikesyo
1
1.5k
RenovateによるiOSライブラリーの自動更新
ikesyo
2
4.1k
2019年のSwiftモック事情
ikesyo
3
8.7k
5分でわかる!Xcode 11から使えるXCFrameworks
ikesyo
2
4.1k
Travis CIのBuild Matrixを活用して、Swift製ライブラリをLinux対応させる
ikesyo
3
2.7k
Other Decks in Programming
See All in Programming
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
500
Java 27新機能 / Java 27 new features
kishida
2
170
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
390
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
130
Jetpack Compose メカニズム
skydoves
0
470
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
210
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
440
Are APIs Still Relevant in the AI Era?
soyuka
0
310
Webの地図
yosuke_furukawa
PRO
6
4.7k
AHC070解法紹介
eijirou
0
130
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
360
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
120
Featured
See All Featured
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.1k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
440
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
490
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Chasing Engaging Ingredients in Design
codingconduct
0
340
Mobile First: as difficult as doing things right
swwweet
225
10k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
The SEO identity crisis: Don't let AI make you average
varn
0
560
Heart Work Chapter 1 - Part 1
lfama
PRO
10
36k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
830
Transcript
How to Mock Protocols in Swift Sho Ikeda / @ikesyo
Mobile Act KYOTO #1 2019-04-19 Fri #MobileAct
Sho Ikeda / @ikesyo • ͍͚͠ΐʔʗా ᠳ • ͯͳ@ژ •
εϚʔτϑΥϯΞϓϦ։ൃ • iOS / Android / React Native • Swiftίϛολʔ • https://twitter.com/ikesyo • https://github.com/ikesyo
How to Mock Protocols in Swift • Manually ! •
Sourcery: AutoMockable • SwiftyMocky • Cuckoo
Sourcery: AutoMockable • https://github.com/krzysztofzablocki/Sourcery • Mocks.md • AutoMockable.stencil • ରͱͳΔͷ
• AutoMockable ͱ͍͏໊લͷϓϩτίϧʹ४ڌ • // sourcery: AutoMockable ͷΞϊςʔγϣϯίϝϯτͷ ༩
Sourcery: AutoMockable • ϝιου • ݺΕ͔ͨͲ͏͔ͷνΣοΫ: ճ͔Βͳ͍ • ҾλϓϧͷϓϩύςΟ: ݺΕͨҾͷνΣοΫ
• Γઃఆ༻ͷϓϩύςΟ • ϓϩύςΟఆٛ • ͔ͳΓγϯϓϧ • ֎෦Ҿ໊͕ಉҰɾܕҧ͍ͷΦʔόʔϩʔυNG • ϓϩτίϧఆٛதʹ֎෦ϑϨʔϜϫʔΫͷܕ͕ొ͢Δ߹ɺࣗͰςϯϓϨʔτΛมߋ ͯ͠importΛॻ͖Ճ͑Δඞཁ͕͋Δ
Sourcery: AutoMockable • try! Swift Tokyo 2018 - Investing time
into developer tools and experience • https://www.youtube.com/watch?v=yAQQ0cIxSF8 • SourceryΛ׆༻ͯ͠ςετΛॻ͘ϋʔυϧΛԼ͛Δ - Qiita
SwiftyMocky • https://github.com/MakeAWishFoundation/SwiftyMocky • Sourceryϕʔε • AutoMockableͷϚʔΫΛͦͷ··༻ • ΑΓߴػೳͳςϯϓϨʔτͱϥϯλΠϜ •
SourceryͷAutoMockableςϯϓϨʔτ͔ΒҠߦ͍͢͠
SwiftyMocky • δΣωϦΫεͷαϙʔτ • staticϝϯόʔͷαϙʔτ • ઃఆϑΝΠϧͰimport͢ΔϑϨʔϜϫʔΫΛࢦఆՄೳ • GivenʹΑΔελϒ •
VerifyʹΑΔϝιουݺͼग़͠ͷݕࠪ • ճҾ • PerformʹΑΔϝιουͷϘσΟͷࠩ͠ସ͑
SwiftyMocky: Given Given(mock, .surname(for name: .value("Johnny"), willReturn: "Bravo")) Given(mock, .surname(for
name: .any, willReturn: "Kowalsky")) print(mock.surname(for: "Johny")) // Bravo print(mock.surname(for: "Mathew")) // Kowalsky print(mock.surname(for: "Joanna")) // Kowalsky
SwiftyMocky: Given Given(mock, .surname(for name: .any, willReturn: "Bravo", "Kowalsky", "Nguyen"))
print(mock.surname(for: "Johny")) // Bravo print(mock.surname(for: "Johny")) // Kowalsky print(mock.surname(for: "Johny")) // Nguyen print(mock.surname(for: "Johny")) // and again Bravo // ...
SwiftyMocky: Verify sut.usersStorage = mockStorage sut.saveUser(name: "Johny", surname: "Bravo") sut.saveUser(name:
"Johny", surname: "Cage") sut.saveUser(name: "Jon", surname: "Snow") Verify(mockStorage, .storeUser(name: .value("Jon"), surname: .value("Snow"))) Verify(mockStorage, 3, .storeUser(name: .any, surname: .any)) Verify(mockStorage, 2, .storeUser(name: .value("Johny"), surname: .any)) Verify(mockStorage, .moreOrEqual(to: 2), .storeUser(name: .matching({ $0.count > 3 }}), surname: .any)) Verify(mockStorage, .never, .deleteUser(id: .any))
SwiftyMocky: Perform Perform( mock, .methodThatTakesCompletionBlock( completion: .any, perform: { completion
in completion(true,nil) } ) )
Cuckoo • https://github.com/Brightify/Cuckoo • ಠࣗͷίʔυδΣωϨʔλʔ • SourceryϕʔεͰͳ͍͕ɺSourceKittenͱStencilΛ͍ͬͯΔͷಉ͡ • Protocol͚ͩ͡Όͳ͘ɺClassαϙʔτ͍ͯ͠Δ •
δΣωϦΫεະαϙʔτ • staticϝϯόʔඇαϙʔτ • ͦͷଞͷػೳSwiftyMockyͱḮ৭ͳͦ͞͏ • ߏจΑΓSwifty͔͠Εͳ͍
Cuckoo stub(mock) { stub in when(stub.greetWithMessage("Hello world")).then { message in
print(message) } } stub(mock) { stub in when(stub.readWriteProperty.get).thenReturn(10) when(stub.readWriteProperty.set(anyInt())).then { print($0) } }
Cuckoo mock.readWriteProperty = 10 mock.readWriteProperty = 20 mock.readWriteProperty = 30
let argumentCaptor = ArgumentCaptor<Int>() verify(mock, times(3)).readWriteProperty.set(argumentCaptor.capture()) argumentCaptor.value // Returns 30 argumentCaptor.allValues // Returns [10, 20, 30]
! Happy Swift Mocking/Testing
WE ARE HIRING https://hatenacorp.jp/recruit/
Thank you Sho Ikeda / @ikesyo