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.6k
Open Source Swift Workshop - Foundation and first party libraries
ikesyo
0
3.4k
Renovateで実現するライブラリの自動更新生活 / Automated Library Updates with Renovate
ikesyo
3
730
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
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4.4k
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
520
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
170
XHTMLが残したもの
yosuke_furukawa
PRO
2
630
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
590
Security issues being discussed on Web Platforms
petamoriken
0
490
Family mrubyの進捗
kishima
1
110
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
1.6k
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
高専キャリア LT 発表内容
crysta1221
6
5.6k
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
330
41k
A Tale of Four Properties
chriscoyier
163
24k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Practical Orchestrator
shlominoach
191
12k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
520
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
440
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
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