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
Unity用MockClientの話
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
いも
June 19, 2018
Technology
1.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unity用MockClientの話
弁当付き社内LT「Jikai Lightning Talks #1」で発表したやつを公開用にちょっと修正したやつです
いも
June 19, 2018
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
660
Unityテスト活動のふりかえり
adarapata
1
710
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
8.9k
Gather.townはいいぞ
adarapata
2
2.5k
宴はいいぞ
adarapata
0
2.3k
わかった気になるモブプログラミング
adarapata
1
190
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.3k
Other Decks in Technology
See All in Technology
おそらく日本で唯一のDevRelインターン生として
husengs7
0
180
ビジネスを止めない技術的負債の返済のための戦略とその手法 - 技術的負債と向き合う / Complexity and Simplicity
soudai
PRO
3
700
Snapshot Testing in Practice: Predictable and Reliable SwiftUI Views
fespinoza
0
200
宣言型部分更新は何を解決しどこへ向かうのか
shuta13
1
320
Loop Engineering これくらいでいい
mikanichinose
0
230
WasmをRustにする
sansantech
PRO
0
200
USB PDを使ってスマホを労わりたい
raspython3
0
160
QA4AI 2026-Oct 生成AI時代の品質保証
tokuty
0
450
不可逆な意思決定を避けながら10億超の顧客データを AI Ready にするデータ基盤設計
baseballyama
1
310
実践!Cloud Run ハンズオン!! / Cloud Run handson
y0hgi
0
230
SREでアラート疲れを 解決しよう!
kairim0
1
230
hackers_champloo_2026_tidb
makocchi
0
140
Featured
See All Featured
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
YesSQL, Process and Tooling at Scale
rocio
174
15k
Mind Mapping
helmedeiros
1
400
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
590
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
The SEO identity crisis: Don't let AI make you average
varn
0
590
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
470
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
620
Are puppies a ranking factor?
jonoalderson
2
4k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.3k
A Tale of Four Properties
chriscoyier
163
24k
Marketing to machines
jonoalderson
1
5.9k
Transcript
Unity 用MockClient のお話 2018/06/19 Jikai Lightning Talks #1
imo です
最近やったこと テスト導入したりしていました。 https://speakerdeck.com/adarapata/unitypurodakut onitesutowodao-ru-siteikumade?slide=1
最近困っていたこと ユニットテストは導入できたが書けないパター ン がある 通信とか挟まるとダメ 通信の結果ごにょごにょとかよくある 通信テスト自体はエディタ拡張している つらい
どう解決するか? ユニットテスト時のみモックしてくれる仕組みが 欲しい Unity にそういう機能はないっぽい 通信をモックするクライアントを実装しよう
API Client の流れ APIClient にRequestBase を投げると通信してくれる var client = new
ApiClient(); client.StartRequest(new FooRequest()); // => Result<FooResponse>
抽象化してみる IHTTPClient を差し替えれればスマー トっぽい でも結構根っこから変えるのは大変そう IApiClient <=> IRequest 間をごまかそう
どうモックするか Moq を利用する https://github.com/moq/moq4 C# 用のモックライブラリ Zenject にOption として付属している .NET
3.5 用と4.6 用どちらも用意してて便利
できあがり テスト用に ApiClientMock クラスを作成。 Mock メソッドでモックできる var mock = new
ApiClientMock(); var mockResponse = new TResponse(); mock.Mock<TRequest, TResponse>().Success(mockResponse); mock.StartRequest(new TRequest()); // => Result<TResponse>
通信が成功したことにする var mock = new ApiClientMock(); var response = new
FooResponse(); mock.Mock<FooRequest, FooResponse>().Success(response); var result = mock.StartRequest(new FooRequest()); result.Response.Content; // => FooResponse
サー バからエラー が返ってきたことにする var mock = new ApiClientMock(); ErrorResponse error
= new ErrorResponse(); mock.Mock<FooRequest, FooResponse>().Failure(error); var result = mock.StartRequest(new FooRequest()); result.Response.ErrorResponse; // => ErrorResponse
通信エラー が発生したことにする var mock = new ApiClientMock(); mock.Mock<FooRequest, FooResponse>().Error(Error.Network); var
result = mock.StartRequest(new FooRequest()); result.ErrorCause; // => Error.Network
バリデー ション モックしてない部分の通信が走ると落ちる => FooRequest はモックされていません。 ex. client.Mock<FooRequest, FooResponse>().Success(res); モックしたレスポンスが適切でなかったら落ちる
=> レスポンスが間違ってます Expected: <FooReponse> But was: <BarResponse> モックしたけど呼ばれなかったら落ちる => FooRequest をモックしましたが呼ばれていません!
用途 通信の結果で処理が変わるFooUseCase を想定 public class FooUseCase { private IApiClient client;
public FooUseCase(IApiClient client) { this.client = client; } public string Run() { var result = client.StartRequest(new FooRequset()); if(result.IsSuccess) { return result.Response.Content.Foo; } else { return "NG"; } } }
テストコー ド public class FooUseCaseTest { private IApiClient client; [Test]
public void Run_Success() { client = new ApiClientMock(); var response = new FooResponse { Foo = "OK" }; client.Mock<FooRequest, FooReponse>() .Success(response); var useCase = new FooUseCase(client); Assert.AreEqual("OK", useCase.Run()); } } ApiClientMock を外から流し込めば意図した挙動にな る
Moq 便利 通信部分を気にせずテストが書ける 「 これテスト書けますよ!」 というアプロー チが しやすくなった モックのやり過ぎに注意 テストしないといけない部分をモックしては本末
転倒 何故モックするのかを考える 通信そのものは別でテストしましょう