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
Unit Testing for an iOS developer
Search
jsa :~
October 11, 2012
Programming
380
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unit Testing for an iOS developer
1st talk of this series.
jsa :~
October 11, 2012
More Decks by jsa :~
See All by jsa :~
CloudKit First Take
jamessa
0
150
Docker in Golang
jamessa
10
2.1k
Better Design Through Testing
jamessa
3
430
Other Decks in Programming
See All in Programming
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
5.7k
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
110
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
160
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
170
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
120
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
410
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
380
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
270
Featured
See All Featured
Building Applications with DynamoDB
mza
96
7.1k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
The Cult of Friendly URLs
andyhume
79
6.9k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
330
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
600
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
740
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
430
4 Signs Your Business is Dying
shpigford
187
22k
Transcript
UNIT TESTING jamie sa @JAMEX for an iOS developer Saturday,
October 20, 12
UNIT TESTING IS NOT FOR IOS Unit Testing is awesome
for guaranteeing consistent behavior of infrastructure. - bbum 2005 Saturday, October 20, 12
@implementation BrowseOverflowViewControllerTests { BrowseOverflowViewController *viewController; UITableView *tableView; id<UITableViewDataSource> dataSource; UINavigationController
*navController; SEL realViewDidAppear, testViewDidAppear; SEL realViewWillDisappear, testViewWillDisappear; SEL realUserDidSelectTopic, testUserDidSelectTopic; BrowseOverflowObjectConfiguration *objectConfiguration; } + (void)swapInstanceMethodsForClass: (Class) cls selector: (SEL) sel1 andSelector: (SEL) sel2 { Method method1 = class_getInstanceMethod(cls, sel1); Method method2 = class_getInstanceMethod(cls, sel2); method_exchangeImplementations(method1, method2); } Method Swizzling Saturday, October 20, 12
@implementation BrowseOverflowViewControllerTests { BrowseOverflowViewController *viewController; UITableView *tableView; id<UITableViewDataSource> dataSource; UINavigationController
*navController; SEL realViewDidAppear, testViewDidAppear; SEL realViewWillDisappear, testViewWillDisappear; SEL realUserDidSelectTopic, testUserDidSelectTopic; BrowseOverflowObjectConfiguration *objectConfiguration; } + (void)swapInstanceMethodsForClass: (Class) cls selector: (SEL) sel1 andSelector: (SEL) sel2 { Method method1 = class_getInstanceMethod(cls, sel1); Method method2 = class_getInstanceMethod(cls, sel2); method_exchangeImplementations(method1, method2); } Method Swizzling in runtime.h OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0); Saturday, October 20, 12
UNIT TESTING SLOWS DOWN DEVELOPMENT PROCESS Unit Testing is core
part of ANY Agile best practice. Saturday, October 20, 12
Saturday, October 20, 12
UNIT TESTS ARE A FORM OF SAMPLE CODE Saturday, October
20, 12
- (void)testArticleWithIncompletedAttachmentShouldBeDecorated { NSArray *changedArticleReps = [self loadDataFile:@"PostWithIncompleteAttachmentInformation"]; STAssertNotNil(changedArticleReps, @"should
be a JSON array"); NSArray *touchedArticles = [WAArticle insertOrUpdateObjectsUsingContext:context withRemoteResponse:changedArticleReps usingMapping:nil options:IRManagedObjectOptionIndividualOperations]; WAArticle *article = [touchedArticles objectAtIndex:0]; STAssertEquals((NSUInteger)20, [article.files count], @"attachments should be decorated to 20"); NSFetchRequest *fetchRequest = [[WADataStore defaultStore] newFetchRequestForFilesInArticle:article]; NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:article.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; NSError *fetchError = nil; if (![fetchedResultsController performFetch:&fetchError]) NSLog(@"Error fetching: %@", fetchError); STAssertEquals((NSUInteger)20, [[fetchedResultsController fetchedObjects] count], @"Should be 20."); for (WAFile *photo in [fetchedResultsController fetchedObjects]) { STAssertEqualObjects(@"public.jpeg", photo.resourceType, @"Type must be jpeg."); STAssertEqualObjects(@"image", photo.remoteResourceType, @"Must be an image."); STAssertNotNil(photo.smallThumbnailURL, @"Small thumbnail required"); STAssertNotNil(photo.thumbnailURL, @"Medium thumbnail required"); } Saturday, October 20, 12
USE OCMOCK OCMATCHER Saturday, October 20, 12
USE OCMOCK OCHAMCREST Saturday, October 20, 12
- (void)testOpenConnectionFail { __block BOOL complete = NO; __weak WAWebSocket
*wSocket = webSocket; [[[mockSocket expect] andDo:^(NSInvocation *invocation) { [wSocket performSelector:@selector(webSocket:didFailWithError:) withObject:nil }] open]; [webSocket replaceWebSocketConnection:(SRWebSocket*)mockSocket]; [webSocket openConnectionOnSucces:^{ complete = YES; STFail(@"Websocket connection should fail to be opened."); } onFailure:^(NSError *error) { complete = YES; // success }]; while (complete == NO && [asyncWaitUntil timeIntervalSinceNow] > 0) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:asyncWaitUntil]; } if (complete == NO) { STFail(@"Websocket connection should be opened on time."); } [mockSocket verify]; } Saturday, October 20, 12
IT’S FASTER THAN WRITING CODE WITHOUT TESTS Saturday, October 20,
12
It’s your turn NOW. Saturday, October 20, 12