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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
3.6k
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
180
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
550
Claude Team Plan導入・ガイド
tk3fftk
0
240
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
180
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
250
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
0
330
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
570
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
400
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
210
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
130
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
170
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
Done Done
chrislema
186
16k
Paper Plane (Part 1)
katiecoart
PRO
1
9.9k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
980
Being A Developer After 40
akosma
91
590k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
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