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
640
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
750
Xcodeとの最近の付き合い方のはなし / Approach To Xcode
usamik26
2
670
UICollectionView Compositional Layout
usamik26
0
790
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
レベル1の開発生産性向上に取り組む − 日々の作業の効率化・自動化を通じた改善活動
kesoji
0
190
AI コーディングエージェントの時代へ:JetBrains が描く開発の未来
masaruhr
1
140
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
650
生成AI時代のコンポーネントライブラリの作り方
touyou
1
210
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
21
4k
「テストは愚直&&網羅的に書くほどよい」という誤解 / Test Smarter, Not Harder
munetoshi
0
170
A full stack side project webapp all in Kotlin (KotlinConf 2025)
dankim
0
120
Discover Metal 4
rei315
2
130
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
760
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
700
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
120
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
200
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
We Have a Design System, Now What?
morganepeng
53
7.7k
Raft: Consensus for Rubyists
vanstee
140
7k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
Thoughts on Productivity
jonyablonski
69
4.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
54k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
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 ͚ͷϥΠϒϥϦཉ͍͠ʣ