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
Oxcを導入して開発体験が向上した話
yug1224
4
290
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
320
RTSPクライアントを自作してみた話
simotin13
0
510
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
450
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
620
ふつうのFeature Flag実践入門
irof
7
3.6k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
Old Dog, New Tricks: The Java 25 Reinvention - JNation
bazlur_rahman
0
150
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
450
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
Claspは野良GASの夢をみるか
takter00
0
170
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
17
6.1k
Featured
See All Featured
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.6k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
Design in an AI World
tapps
1
230
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
270
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
290
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Site-Speed That Sticks
csswizardry
13
1.2k
The Spectacular Lies of Maps
axbom
PRO
1
790
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
210
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
Rails Girls Zürich Keynote
gr2m
96
14k
Designing Powerful Visuals for Engaging Learning
tmiket
1
400
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