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
Unity用MockClientの話
Search
いも
June 19, 2018
Technology
1.5k
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
630
Unityテスト活動のふりかえり
adarapata
1
660
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
8.7k
Gather.townはいいぞ
adarapata
2
2.4k
宴はいいぞ
adarapata
0
2.2k
わかった気になるモブプログラミング
adarapata
1
170
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.3k
Other Decks in Technology
See All in Technology
自律型AIエージェントは何を破壊するのか
kojira
0
140
Disciplined Vibes: Scaling AI-Assisted Engineering
sheharyar
0
110
2026TECHFRESH畢業分享會 - AI 時代的人生存檔點
line_developers_tw
PRO
0
590
非定型業務をAI slackbotで自動化する ~ 社内要望を自動壁打ちするbotを作った ~/automating-ad-hoc-work-with-ai-slackbot
shibayu36
0
560
AIっぽい文章を採点して人間らしく直すアプリを作ってみた
yama3133
2
110
Amazon Bedrock AgentCore ワークショップ JAWS UG TOHOKU / amazon-bedrock-agentcore-workshop-jawsug-tohoku-2026
gawa
9
580
"何を作るか"を任される エンジニアは、どう育つのか
yutaokafuji
1
500
Bucharest Tech Week 2026 - Reinventing testing practices in the AI era
edeandrea
PRO
1
130
運用を見据えたAIエージェント設計実践
amacbee
1
3.5k
脆弱性対応、どこで線を引くか
rymiyamoto
0
290
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development with AI-DLC
yoshidashingo
0
160
「速く作る」から「正しく作る」へ ─ 生成AI時代の開発フロー改革の ロードマップと実行 ─
starfish719
0
9.5k
Featured
See All Featured
A better future with KSS
kneath
240
18k
Leo the Paperboy
mayatellez
7
1.8k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
330
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
330
WENDY [Excerpt]
tessaabrams
11
38k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Discover your Explorer Soul
emna__ayadi
2
1.1k
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 便利 通信部分を気にせずテストが書ける 「 これテスト書けますよ!」 というアプロー チが しやすくなった モックのやり過ぎに注意 テストしないといけない部分をモックしては本末
転倒 何故モックするのかを考える 通信そのものは別でテストしましょう