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
630
Apple HIG 正式名称クイズ結果発表 / HIG Quiz Result
usamik26
0
180
ゆめみ大技林製作委員会の立ち上げの話 / daigirin project
usamik26
0
330
@ViewLoadingプロパティラッパの紹介と自前で実装する方法 / @ViewLoading property wrapper implementation
usamik26
0
480
これからUICollectionViewを実践活用する人のためのガイド / Guide to UICollectionView
usamik26
1
740
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
670
UICollectionView Compositional Layout
usamik26
0
780
Coding Swift with Visual Studio Code and Docker
usamik26
0
510
Swift Extension for Visual Studio Code
usamik26
2
1k
Other Decks in Programming
See All in Programming
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
190
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
250
Development of an App for Intuitive AI Learning - Blockly Summit 2025
teba_eleven
0
120
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
120
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
840
FormFlow - Build Stunning Multistep Forms
yceruto
1
190
CursorはMCPを使った方が良いぞ
taigakono
0
130
生成AIで日々のエラー調査を進めたい
yuyaabo
0
610
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
380
Javaのルールをねじ曲げろ!禁断の操作とその代償から学ぶメタプログラミング入門 / A Guide to Metaprogramming: Lessons from Forbidden Techniques and Their Price
nrslib
3
2k
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
2
600
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
610
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
A Modern Web Designer's Workflow
chriscoyier
693
190k
BBQ
matthewcrist
89
9.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Designing for Performance
lara
609
69k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Adopting Sorbet at Scale
ufuk
77
9.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Fireside Chat
paigeccino
37
3.5k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Balancing Empowerment & Direction
lara
1
340
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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 ͚ͷϥΠϒϥϦཉ͍͠ʣ