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
Moq
Search
USAMI Kosuke
December 09, 2017
Programming
1
1.7k
Moq
Mobile Act Osaka #2
https://mobileact.connpass.com/event/68892/
USAMI Kosuke
December 09, 2017
Tweet
Share
More Decks by USAMI Kosuke
See All by USAMI Kosuke
Onsager代数とその周辺 / Onsager algebra tsudoi
usamik26
0
380
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
69
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
250
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
360
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
610
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
530
UICollectionView Compositional Layout
usamik26
0
550
Coding Swift with Visual Studio Code and Docker
usamik26
0
350
Swift Extension for Visual Studio Code
usamik26
2
760
Other Decks in Programming
See All in Programming
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
310
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
260
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
310
スキーマ駆動開発による品質とスピードの両立 - 私達は何故、スキーマを書くのか
kentaroutakeda
0
170
Milestoner
bkuhlmann
1
410
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
0
510
Git Lint
bkuhlmann
4
750
雑に思考を整理する技術と効能
konifar
58
29k
Elm 0.19.0 Changes
bkuhlmann
0
490
Apache Hive 4 on Treasure Data
ryukobayashi
0
150
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
270
dbtのドメイン分割による データ基盤の改善とDigdagとの連携
sakama
0
150
Featured
See All Featured
Building Adaptive Systems
keathley
31
1.9k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Art, The Web, and Tiny UX
lynnandtonic
289
19k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
121
39k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
GraphQLとの向き合い方2022年版
quramy
32
12k
Learning to Love Humans: Emotional Interface Design
aarron
267
39k
Six Lessons from altMBA
skipperchong
21
3k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
In The Pink: A Labor of Love
frogandcode
138
21k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
Transcript
Moq Mocking Framework for .NET USAMI Kosuke Fenrir Inc.
ࣗݾհ » Ӊࠤݟެี (@usamik26) » ϑΣϯϦϧגࣜձࣾ » iOS (Swift) »
Xamarin.iOS (C#)
ςετμϒϧ
୯ମςετʹ͓͚ΔҎԼͷΑ͏ͳ » ςετରͷґଘΦϒδΣΫτ͕ɾɾɾ » ωοτϫʔΫ௨৴͕ඞཁʁ » σʔλϕʔεΞΫηε͕ඞཁʁ » ͦͷ··Ͱ୯ମςετΛ࡞Γʹ͍͘
ςετμϒϧ » ࣮ࡍͷΦϒδΣΫτͷΘΓΛ༻ҙ͢Δ » ςετμϒϧʹςετ༻ͷΦϒδΣΫτ » ׂʹԠͯ͡ɺελϒɺϞοΫɺϑΣΠΫɺͳͲʹྨͰ͖Δ » ʢͦΕΒͷҧ͍ʹ͍ͭͯ͜͜ͰׂѪʣ
None
యܕతͳςετμϒϧͷ࣮ » ຊͱಉ͡ I/F Λ࣋ͭ » ௨৴ͳͲͷৼΔ͍͠ͳ͍ » ಛఆͷೖྗ͕͞ΕΔͱɺಛఆͷग़ྗΛฦ͢
ςετμϒϧΛ࣮͢Δͱ͖ͷ » ຊͱಉ͡ I/F Λ࣮͢Δͷͬͯ݁ߏΊΜͲ͍͘͞ » ୯ମςετͷͨΊʹͦ͜·ͰؤுΔͷʁ » αϙʔτͯ͘͠ΕΔϥΠϒϥϦ͕͋Δͱخ͍͠
ϥΠϒϥϦ » OCMock : iOS (Objective-C) » Mockito : Android
(Java / Kotlin) » Moq : .NET / Xamarin (C#)
Moq
Moq » Mocking Framework for .NET » https://github.com/moq/moq4 » ൃԻʮϞοΫʯ͔ʮϞοΩϡʯ
Moq ͷ͍ํ public interface IHoge { bool DoSomething(string value); }
var mock = new Mock<IHoge>(); // interface ͕ࣗಈతʹ࣮͞ΕΔ mock.Object.DoSomething("abc"); // -> false
ҙ (1) interface ͕ඞཁ » ஔ͖͍͑ͨΦϒδΣΫτͷ interface Λఆٛ͢Δ » ຊͷ
class ͦͷ interface Λ࣮͢Δ » ςετμϒϧͦͷ interface Λ࣮͢Δ » ࢀߟ : ຊͷ class Λܧঝͯ͠ςετμϒϧʹ͢Δख͋Δ
ྫɿݻఆΛฦ͢ϓϩύςΟ public interface IHoge { string Name { get; }
} var mock = new Mock<IHoge>(); mock.SetupGet(x => x.Name) .Returns("xyz"); var name = mock.Object.Name; // -> "xyz"
ྫɿSet ՄೳͳϓϩύςΟ public interface IHoge { string Name { get;
set; } } var mock = new Mock<IHoge>(); mock.SetupProperty(x => x.Name, "xyz"); mock.Object.Name = "123"; var name = mock.Object.Name; // -> "123"
ྫɿಛఆͷೖྗʹಛఆͷग़ྗΛฦ͢ public interface IHoge { bool DoSomething(string value); } var
mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething("abc")) .Returns(true); mock.Object.DoSomething("abc"); // -> true
ྫɿೖྗͷ݅Λࢦఆ͢Δ public interface IHoge { bool DoSomething(string value); } var
mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething(It.Is<string>(s => s.Length < 10))) .Returns(true); mock.Object.DoSomething("abc"); // -> true
ྫɿೖྗ༰ʹԠͨ͡ग़ྗΛฦ͢ public interface IHoge { string DoSomething(string value); } var
mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething(It.IsAny<string>())) .Returns(s => s.ToUpper()); mock.Object.DoSomething("abc"); // -> "ABC"
ςετίʔυ͔Βͷ͍ํ » ࣄલ४උ » Mock ΠϯελϯεΛੜ » Setup ΛͣΒͣΒͱॻ͘ »
mock.Object Λऔಘ͢Δ » ςετର࣮ࡍͷΦϒδΣΫτͷΘΓʹ mock.Object Λ͏
ҙ (2) ґଘΦϒδΣΫτͷࠩ͠ସ͑ » ςετର class Ͱͳ͘ interface ʹґଘ͢Δ͜ͱ »
ຊͷΦϒδΣΫτΛςετμϒϧʹࠩ͠ସ͑ΒΕΔΑ͏ʹ͢Δ͜ͱ » ίϯετϥΫλͰ͢ɺDI ίϯςφΛ͏ɺɾɾɾ
Moq Ԡ༻ฤ » ϓϩύςΟͷఆٛΛ͍͍ͪͪॻ͖ͨ͘ͳ͍ » ྫ֎ͷςετΛ͍ͨ͠ » ಛఆͷॲཧ͕ݺΕΔͨͼʹฦ͢ΛมԽ͍ͤͨ͞ » ಛఆͷॲཧ͕ͪΌΜͱݺΕ͔ͨ֬ೝ͍ͨ͠
ྫɿϓϩύςΟͷఆٛΛָʹ͢Δ public interface IHoge { string Name { get; set;
} } var mock = new Mock<IHoge>(); mock.SetupAllProperties();
ྫɿྫ֎Λൃੜͤ͞Δ public interface IHoge { bool DoSomething(string value); } var
mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething("abc")) .Throws<InvalidOperationException>();
ྫɿฦ͢ΛมԽͤ͞Δ var mock = new Mock<IHoge>(); mock.SetupSequence(x => x.DoSomething("abc")) .Returns(2)
.Returns(1) .Returns(0) .Throws<InvalidOperationException>();
ྫɿݺͼग़͞Ε͔ͨͲ͏͔Λݕূ͢Δ var mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething("abc")) .Returns(true);
mock.Verify(x => x.DoSomething("abc"), Times.AtLeastOnce());
ྫɿݺͼग़͞ΕͨࡍͷॲཧΛهड़͢Δ var mock = new Mock<IHoge>(); mock.Setup(x => x.DoSomething("abc")) .Callback(()
=> Console.WriteLine("Before returns")) .Returns(true) .Callback(() => Console.WriteLine("After returns"));
Γ͗͢ҙ » ڽΓͩ͢ͱ͍Ζ͍ΖͰ͖Δ͚Ͳɾɾɾ » ͋͘·Ͱςετ༻ͷΦϒδΣΫτ » ୯ମςετͷͱ͖͚ͩ͑Ε͍͍ » ຊʹͳΔඞཁͳ͍
·ͱΊ » ςετμϒϧΛָʹ࡞Δͷʹ Moq ศར » ʢ༨ஊɿSwift ͚ͷϥΠϒϥϦཉ͍͠ʣ