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
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
340
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
840
ALB ログから Trace を気合で繋げる技術
fohte
7
870
RSSとCodexを使ってX投稿自動化してみた
ochtum
0
110
PyO3 で既存 Python 評価器を Rust core 化する ー wasm-bindgen でブラウザにも配るための設計
kdash
1
430
Foundry Localでエージェント開発
seosoft
0
140
バグを直したら useEffect が消えた
colorful12
3
840
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
740
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
110
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4.3k
AIの中の人になってみる
htkym
0
170
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
130
Featured
See All Featured
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
370
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
230
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
200
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
How to Talk to Developers About Accessibility
jct
2
540
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
340
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
250
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
310
How to Ace a Technical Interview
jacobian
281
24k
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