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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
550
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
SlackアプリとLambdaの 連携を構築した話
pawn_4_s
1
110
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
770
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
170
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
310
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.5k
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
220
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.2k
Google Apps Script で Ruby を動かす
kawahara
0
150
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
340
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1369
210k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
Context Engineering - Making Every Token Count
addyosmani
9
1k
HDC tutorial
michielstock
2
790
Leo the Paperboy
mayatellez
8
2.1k
Exploring anti-patterns in Rails
aemeredith
3
460
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Marketing to machines
jonoalderson
1
5.7k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Prompt Engineering for Job Search
mfonobong
0
400
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