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
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
130
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
190
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
140
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
200
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
160
Lessons from Spec-Driven Development
simas
PRO
0
240
Creating Composable Callables in Contemporary C++
rollbear
0
180
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
380
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.6k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
630
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
180
Featured
See All Featured
How to Talk to Developers About Accessibility
jct
2
270
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
How to make the Groovebox
asonas
2
2.3k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
490
Crafting Experiences
bethany
1
200
The Curse of the Amulet
leimatthew05
2
13k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
750
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
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