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
いも
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
Transcript
16 slides
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 便利 通信部分を気にせずテストが書ける 「 これテスト書けますよ!」 というアプロー チが しやすくなった モックのやり過ぎに注意 テストしないといけない部分をモックしては本末
転倒 何故モックするのかを考える 通信そのものは別でテストしましょう
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
670
Unityテスト活動のふりかえり
adarapata
1
720
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
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
BALLAS Technology Deck
ballas_inc
0
650
81天的 AI Agent 軟體開發團隊治理實務分享
regionbbs
0
170
あの日、見つかっていなかったらどうなっていたか
kazzpapa3
2
220
サーバーラックは作る時代! 3Dプリンターで作る10インチラックのススメ
jacopen
1
170
Go初心者のクラウドエンジニアが、デプロイを通して旨味を知っていく話
ryudai
1
220
Kiro 入門 (1 つのエージェントを、どの画面からでも) | 目指せ Kiro マスター 01
inariku
PRO
6
1.8k
Linuxネットワークスタック進化の軌跡: 過去10年の歩みと最新トレンド
orimanabu
4
710
Claude Codeで新規事業を伸ばせるか? Zennの試行錯誤の記録
wadayusuke
2
200
MCPでネットワーク障害調査を自動化する前に直すべき3つのこと
yama3133
0
380
Sonnetだけで業務はまわる?
kintotechdev
1
130
AI対話型分析を作成し、リリースするまでの話 / The Road to Release: Buiding AI Conversational Analytics
yappli_developers
0
140
速くなった開発の効果をどう推し量るのか
bulbulpaul
0
120
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Between Models and Reality
mayunak
4
580
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Typedesign – Prime Four
hannesfritz
42
3.2k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
370
Large-scale JavaScript Application Architecture
addyosmani
516
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.7k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k