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.8k
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
530
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
110
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
280
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
430
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
690
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
610
UICollectionView Compositional Layout
usamik26
0
680
Coding Swift with Visual Studio Code and Docker
usamik26
0
450
Swift Extension for Visual Studio Code
usamik26
2
910
Other Decks in Programming
See All in Programming
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
RubyLSPのマルチバイト文字対応
notfounds
0
120
受け取る人から提供する人になるということ
little_rubyist
0
230
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
CSC509 Lecture 13
javiergs
PRO
0
110
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Arm移行タイムアタック
qnighy
0
330
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Better Code Design in PHP
afilina
PRO
0
130
Featured
See All Featured
KATA
mclloyd
29
14k
The Cult of Friendly URLs
andyhume
78
6k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
A Tale of Four Properties
chriscoyier
156
23k
Visualization
eitanlees
145
15k
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 ͚ͷϥΠϒϥϦཉ͍͠ʣ