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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
210
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
220
Performance Engineering for Everyone
elenatanasoiu
0
250
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
150
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
230
Webフレームワークの ベンチマークについて
yusukebe
0
190
JavaDoc 再入門
nagise
1
440
はてなアカウント基盤 State of the Union
cockscomb
1
1.2k
どこまでゆるくて許されるのか
tk3fftk
0
290
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
320
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
620
Lessons from Spec-Driven Development
simas
PRO
0
240
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
160
The Curious Case for Waylosing
cassininazir
1
420
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
170
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
260
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
170
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
230
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
290
Paper Plane (Part 1)
katiecoart
PRO
0
9.4k
Paper Plane
katiecoart
PRO
1
52k
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