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
XCTestでAPIのテストコードを書く
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
t.goda
April 22, 2022
Technology
420
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
XCTestでAPIのテストコードを書く
MockingJayを利用してXCTestでのAPIテスト
t.goda
April 22, 2022
Transcript
12 slides
XCTestでAPIをテストする 2022/04/22
長年やれと言われていた • 某アプリケーションの単体テスト • 重要な機能の単体テストはコーディングしていた • APIに関連するテストは行っていなかった • 何故? •
どうやるのか考えたことなかった • スタブ作んなきゃいけない • Response Handlingの処理が某アプリではめんどくさい実装になってる • Delegate使って処理を委譲してるため • >ライブラリいれて実装したりする必要がある • それらによってなんか怖い、めんどくさいという思い
APIのテストコードを書かなければ • 次回から単体テストの強化が必須事項 • あと自分の心理的な安全性を保つため • 技術的な関心 •時間があった
どう実装するのか • XcodeのプロジェクトにはデフォルトでXCTestがある • XCTest • Swift、Objective-Cで利用できるテストフレーム • Create and
run unit tests, performance tests, and UI tests for your Xcode project. • らしい • 今回はXCTestを利用する
ライブラリの選定 • Mockingjay • Swift向けHTTPリクエストのスタブを作成してくれるライブラリ • NSURLConnectionを利用していること前提 • AlamoFireというライブラリを利用しているが、こいつはNSURLConnection使ってる ので問題ない
• https://github.com/kylef/Mockingjay
罠その1 • Swift5だとビルドとおんない • は???? • コードの書き換えが必要 • 修正PRはマージされているが、そのバージョンがreleaseされてない •
なんでやねん!!!!
実装する stub(http(.get, uri: https://~~, json(profile)) と wait(for: [exp!], timeout: 10)
が今回のキモ
スタブを作成する • stub(_ matcher: @escaping Matcher, delay: TimeInterval? = nil,
_ builder: @escaping Builder) -> Stub • メソッド名からわかるように、スタブ生成メソッド • MatcherにマッチしたHTTP/HTTPS通信をハンドリングして、builderを返却する •すごい!!!!!こいつがほしかっ た!!!
罠その2 • このままだと非同期処理(通信処理)が終わる前にテストがおわるので ウェイトをかける • wait(for expectations: [XCTestExpectation], timeout seconds:
TimeInterval) • timeoutまでウェイトをかけるメソッド • expectationsは、このパラメーターがfulfillされるのを待つ
実装する2 • レスポンスを受ける側 キモ: exp!.fulfill()とlatestVersionの中身
罠その3 • XCTtestにこの辺を入れたあと通常ビルドすると起動時にクラッシュする • 原因 • MockingJayを通常の方のビルド環境にいれると死ぬ • テスト用ライブラリだかららしい •
Rx系のテストツールでそういうことがあるというのを見つけたので試したら動いた
まとめ • 自力でAPI周りの単体テストはめんどくさい • でもXCTestを利用してAPIのテストをすることはできる • ライブラリを利用してよりより単体テストライフを • Stubさえ作れればこっちのもんでござる
More Decks by t.goda
See All by t.goda
Distribute Binary Framework with Swift Package Manager
nivisunder7
0
150
App Clipについて
nivisunder7
1
89
Swift5.0 ABI安定化について
nivisunder7
0
400
SwiftUIの紹介
nivisunder7
0
47
Watch Kitのはまりどころ
nivisunder7
0
71
Docker in Docker Tips
nivisunder7
0
67
iOS11のここが素晴らしい
nivisunder7
0
94
iPhoneX時代のレイアウト
nivisunder7
0
110
SpriteKitでゲーム開発
nivisunder7
0
120
Other Decks in Technology
See All in Technology
個人ASを取得して、自宅Kubernetes上のVMをグローバルIPで公開した話
munenick
0
230
AIエージェントのアーキテクチャ設計
nsatoh
0
540
RAGの改善を速く回すための評価基盤
yosukeyoshida
6
3.6k
PHPで家庭用ゲーム機の専用コントローラーをMIDIデバイスとして扱ってみる
wind111
0
200
AWS Well-Arhitected Frameworkセキュリティの柱 について
miu_crescent
PRO
2
260
Kiro Enterprise Management: 管理者が 1 か所で決め、全員の Kiro に届ける
inariku
PRO
0
100
そのイメージ、どのCIが作った? Artifact Attestationsで来歴を固定する / Which CI system created that image? Pinning provenance with Artifact Attestations
chmikata
0
270
メール到達性を支える、リンク先URLのドメイン評価の扱い方
wind111
0
200
Taking back control of your AI development
inesmontani
PRO
0
310
パスワードのない鍵で、 データを手元に置く
ginjihayashi
0
400
社内アプリ基盤「Quartz」による、 非エンジニアとAIに本番デプロイを 任せても壊れないプラットフォームの設計
staka121
PRO
1
230
Terraform 0 to 100:100個以上のリポジトリを常に最新にし続けるための自動化
sue445
3
1.4k
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.5k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
2
540
Art, The Web, and Tiny UX
lynnandtonic
304
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
580
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
410
We Have a Design System, Now What?
morganepeng
55
8.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Speed Design
sergeychernyshev
33
2.3k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
1k
Optimizing for Happiness
mojombo
378
72k
Build your cross-platform service in a week with App Engine
jlugia
234
19k