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.9k
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
600
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
160
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
310
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
460
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
720
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
650
UICollectionView Compositional Layout
usamik26
0
750
Coding Swift with Visual Studio Code and Docker
usamik26
0
490
Swift Extension for Visual Studio Code
usamik26
2
1k
Other Decks in Programming
See All in Programming
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
240
SLI/SLOの設定を進めるその前に アラート品質の改善に取り組んだ話
tanden
2
660
AtCoder Heuristic First-step Vol.1 講義スライド(山登り法・焼きなまし法編)
takumi152
3
950
今から始めるCursor / Windsurf / Cline
kengo_hayano
0
110
Node.js, Deno, Bun 最新動向とその所感について
yosuke_furukawa
PRO
6
3k
複数ドメインに散らばってしまった画像…! 運用中のPHPアプリに後からCDNを導入する…!
suguruooki
0
410
eBPF Updates (March 2025)
kentatada
0
120
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
11
3.3k
爆速スッキリ! Rspack 移行の成果と道のり - Muddy Web #11
dora1998
0
140
‘무차별 LGTM~👍’만 외치던 우리가 ‘고봉밥 코드 리뷰’를?
hannah0731
0
510
安全に倒し切るリリースをするために:15年来レガシーシステムのフルリプレイス挑戦記
sakuraikotone
5
2.1k
マルチアカウント環境での、そこまでがんばらない RI/SP 運用設計
wa6sn
0
240
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
What's in a price? How to price your products and services
michaelherold
244
12k
Producing Creativity
orderedlist
PRO
344
40k
For a Future-Friendly Web
brad_frost
176
9.6k
How to train your dragon (web standard)
notwaldorf
91
5.9k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Typedesign – Prime Four
hannesfritz
41
2.6k
Building Applications with DynamoDB
mza
94
6.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
460
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 ͚ͷϥΠϒϥϦཉ͍͠ʣ