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
Practical examples of using stubs and mocks in ...
Search
Victor Ilyukevich
June 29, 2014
Programming
390
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Practical examples of using stubs and mocks in unit tests
Mobile Optimized 2014
Victor Ilyukevich
June 29, 2014
More Decks by Victor Ilyukevich
See All by Victor Ilyukevich
Testing in iOS
yas375
0
260
Other Decks in Programming
See All in Programming
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.2k
JavaDoc 再入門
nagise
0
290
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
450
柔軟なPDFレイアウトエディタを支える型システム設計 — Discriminated UnionとConditional Typeの実践
minako__ph
4
1.4k
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
130
Oxlintのカスタムルールの現況
syumai
6
1k
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
CSC307 Lecture 17
javiergs
PRO
0
320
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
770
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
450
Featured
See All Featured
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
570
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
770
Documentation Writing (for coders)
carmenintech
77
5.4k
Site-Speed That Sticks
csswizardry
13
1.2k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
140
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
160
The Spectacular Lies of Maps
axbom
PRO
1
790
A Tale of Four Properties
chriscoyier
163
24k
Transcript
Practical examples of using stubs and mocks in unit tests
Victor Ilyukevich @yas375
– Martin Fowler “There is a difference in that the
stub uses state verification while the mock uses behavior verification.” http://martinfowler.com/articles/mocksArentStubs.html
Stubs • Stubs drive the production code through different paths
by returning predefined values in response to method calls from SUT (System Under Test). • You verify state of your SUT.
enabledRemoteNotificationTypes it(@"is Allowed", ^{ id value = theValue(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound);
! UIApplication *app = [UIApplication sharedApplication]; [app stub:@selector(enabledRemoteNotificationTypes) andReturn:value]; ! [[theValue(manager.status) should] equal:theValue(CZAllowed)]; });
Current date context(@"when current minute is 44", ^{ it(@"creates an
event that starts on the next hour", ^{ NSDate *fakeNow = [NSDate dateFromYear:2012 month:1 day:14 hour:14 minute:44]; [NSDate stub:@selector(date) andReturn:fakeNow]; ! [event setupDefaultValues]; ! [[event.startsAt should] equal:[NSDate dateFromYear:2012 month:8 day:23 hour:15 minute:00]]; }); });
NSCalendar context(@"when recurs on Sunday, Monday, Tuesday and Friday", ^{
! it(@"returns 'Sun, Mon, Tue, Fri'", ^{ [[reminder.displayShortRecurrenceDays should] equal:@"Sun, Mon, Tue, Fri"]; }); ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! }); });
NSCalendar context(@"when recurs on Sunday, Monday, Tuesday and Friday", ^{
context(@"in US", ^{ it(@"returns 'Sun, Mon, Tue, Fri'", ^{ [[reminder.displayShortRecurrenceDays should] equal:@"Sun, Mon, Tue, Fri"]; }); }); ! context(@"in Russia", ^{ beforeEach(^{ NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"]; calendar.firstWeekday = 2; ! [NSCalendar stub:@selector(currentCalendar) andReturn:calendar]; }); ! it(@"returns 'Пон, Вт, Пт, Вск'", ^{ [[reminder.displayShortRecurrenceDays should] equal:@"пн, вт, пт, вс"]; }); }); });
Mocks • Behaviour verification, not state. • Mock know what
should happen. • You verify that SUT behaves as expected, i.e. calls a method on a mock. • The tests ask the mock if everything went as expected.
sharedInstance it(@"tracks 'Item viewed’ on appearing", ^{ KWMock *analytics =
[KWMock nullMockForClass:[Analytics class]]; [Analytics stub:@selector(sharedInstance) andReturn:analytics]; ! [analytics should] receive:@selector(track:properties:) withArguments:@"Item viewed", @{ @"Type": @"Calendar event" }, nil]; ! TriggerViewAppearing(controller); });
A crazy example of spying* it(@"shows alert 'Can't do this...'",
^{ KWMock *notInitializedAlert = [KWMock nullMockForClass:[UIAlertView class]]; KWCaptureSpy *spy = [notInitializedAlert captureArgument:@selector(initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:) atIndex:1]; ! [UIAlertView stub:@selector(alloc) andReturn:notInitializedAlert]; ! [subject showUnableToShareAlertIfNeeded]; ! [[spy.argument should] equal:@"Can't do this right now..."]; });
Thanks Victor Ilyukevich @yas375