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
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.5k
Open Source Swift Workshop - Foundation and first party libraries
ikesyo
0
3.3k
Renovateで実現するライブラリの自動更新生活 / Automated Library Updates with Renovate
ikesyo
3
720
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
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.7k
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
Go 1.27 における memory allocation の高速化
andpad
0
140
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
180
What's New in Android 2026
veronikapj
0
260
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
330
テーブルをDELETEした
yuzneri
0
140
プロポーザルを書いてもらう
pvcresin
0
300
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
610
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
180
Featured
See All Featured
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
590
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
260
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
490
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
440
Marketing to machines
jonoalderson
1
5.7k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
450
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
330
Building Applications with DynamoDB
mza
96
7.2k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The agentic SEO stack - context over prompts
schlessera
0
860
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