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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
1
210
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
200
「人を評価する AI」の設計と実装
ryoyanara
0
130
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
280
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
400
Google Apps Script で Ruby を動かす
kawahara
0
130
仕様駆動開発の消費期限
watany
4
900
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
470
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
750
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
200
yield再入門 #phpcon
o0h
PRO
0
920
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
440
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Design in an AI World
tapps
1
270
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
420
How to train your dragon (web standard)
notwaldorf
97
6.7k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1k
Abbi's Birthday
coloredviolet
3
9.1k
We Are The Robots
honzajavorek
0
290
Making Projects Easy
brettharned
120
6.7k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
How to build a perfect <img>
jonoalderson
1
5.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
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