Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the...
Transcript of Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the...
![Page 1: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/1.jpg)
Test Driven Development in the iOS World
Part 2 Doug Sjoquist
http://www.sunetos.com@dwsjoquist
Tuesday, August 16, 2011
![Page 2: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/2.jpg)
What Is It?Using automated software
tests to drive the design and development of an application iteratively by writing your tests
BEFOREyou write your code
Tuesday, August 16, 2011
![Page 3: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/3.jpg)
Why?It greatly enhances my
ability to do professional level work
Tuesday, August 16, 2011
![Page 4: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/4.jpg)
TDD Process
“Just Enough” Design
Development Iterations
Review & Refactoring
Tuesday, August 16, 2011
![Page 5: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/5.jpg)
Iterations
Keep each iteration shortShorter cycles
==Quicker feedback
Tuesday, August 16, 2011
![Page 6: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/6.jpg)
Thinking in TDD fashion
Just In Time Design
Tuesday, August 16, 2011
![Page 7: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/7.jpg)
Error Handling
•Special return codes
•Exceptions with try / catch
•NSError
Tuesday, August 16, 2011
![Page 8: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/8.jpg)
// TestGameBoard.m
- (void) testMoveOutsideRange { NSError *error = nil; [gameBoard_ movePlayer:@"playerA" row:-1 col:-1 error:&error]; GHAssertNotNil(error, @"Expected an error");
GHAssertEquals([error code], (NSInteger) kError_invalidBoardPosition, @"Expected kError_invalidBoardPosition");}
{cc014,cc015}Tuesday, August 16, 2011
![Page 9: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/9.jpg)
More GameBoard Tests
• cc016, c017 - testMoveOutsideRange
• cc018, cc019 - testInvalidMove
• cc020, cc021 - testOnlyTwoPlayersAllowed
• cc022, cc023 - testAlternatePlayers
Tuesday, August 16, 2011
![Page 10: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/10.jpg)
Tests for GameEnd StateConditions
in GameBoard
Tuesday, August 16, 2011
![Page 11: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/11.jpg)
// PreconfiguredGameBoard.h// TDD_TicTacToe//// Created by Douglas Sjoquist on 8/13/11.// Copyright 2011 Ivy Gulch, LLC. All rights reserved.//
#import <Foundation/Foundation.h>#import "GameBoard.h"
@interface PreconfiguredGameBoard : GameBoard {}
- (void) setInternalBoard:(NSString*[3][3]) newBoard;- (void) displayBoard;
{cc024}Tuesday, August 16, 2011
![Page 12: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/12.jpg)
- (void) setInternalBoard:(NSString*[3][3]) newBoard { for (int row=0; row<3; row++) { for (int col=0; col<3; col++) { board_[row][col] = newBoard[row][col]; } }}
- (void) displayBoard { NSLog(@"board"); for (int row=0; row<2; row++) { NSLog(@"%@, %@, %@", board_[row][0], board_[row][1], board_[row][2]); }}
@end
{cc025}Tuesday, August 16, 2011
![Page 13: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/13.jpg)
@implementation TestGameBoardEndStates- (void) setUp { [super setUp]; gameBoard_ = [[PreconfiguredGameBoard alloc] init];}
- (void) tearDown { [gameBoard_ release]; [super tearDown];}
- (void) testFirstRowIsWinner { [gameBoard_ setInternalBoard:(NSString *[3][3]) { {@"X", @"X", @"X"}, {nil, nil, nil}, {nil, nil, nil} }]; GHAssertEqualStrings([gameBoard_ winner], @"X", @"X should win this game");}
{cc026}Tuesday, August 16, 2011
![Page 14: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/14.jpg)
// GameBoard.m
- (NSString *) winner { NSString *currentValue = board_[0][0]; if (currentValue && [currentValue isEqualToString:board_[0][1]] && [currentValue isEqualToString:board_[0][2]]){
return currentValue; }
return nil;}
{cc027}Tuesday, August 16, 2011
![Page 15: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/15.jpg)
More GameBoard Tests
• cc028, cc029 - testSecondRowIsWinner
• cc030, cc031 - testFirstColIsWinner
• cc032 cc033 - testSecondColIsWinner
Tuesday, August 16, 2011
![Page 16: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/16.jpg)
Moving through the TDD cycle quickly becomes addicting and encouraging
Tuesday, August 16, 2011
![Page 17: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/17.jpg)
Time for some refactoring
Tuesday, August 16, 2011
![Page 18: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/18.jpg)
// TestGameBoardEndStates.m
...
- (void) testFirstRowIsWinner { [gameBoard_ setInternalBoard:(NSString *[3][3]) { {@"X", @"X", @"X"}, {nil, nil, nil}, {nil, nil, nil} }]; GHAssertEqualStrings([gameBoard_ winner], @"X", @"X should win this game");
}
...
{cc034}Tuesday, August 16, 2011
![Page 19: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/19.jpg)
Remaining End State Tests
• cc035, cc036 - testDiagonalFrom00IsWinner
• cc037, cc038 - testDiagonalFrom02IsWinner
• cc039, cc040 - testDrawOnFullBoardWithNoWinner
Tuesday, August 16, 2011
![Page 20: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/20.jpg)
GameBoard Behavior Is Covered By Tests,
What's Next?
Tuesday, August 16, 2011
![Page 21: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/21.jpg)
Tests to add:
GameViewController:- test that a request to clear the view does indeed clear the view- test that a request to display X or O in a specific location does so- test that we can display a message- test that we can disable user input- test that we can enable user input- test that user input generates a message to the delegate method when appropriate
Things to consider:...
{cc042}Tuesday, August 16, 2011
![Page 22: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/22.jpg)
Develop Tests for GameViewController
Tuesday, August 16, 2011
![Page 23: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/23.jpg)
Hidden assumptions
Tuesday, August 16, 2011
![Page 24: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/24.jpg)
- (void) testOutletsLinked { GHAssertNotNil(gameViewController.messageLabel, @"messageLabel should be assigned"); GHAssertNotNil(gameViewController.grid_0_0, @"grid_0_0 should be assigned");... GHAssertNotNil(gameViewController.grid_2_2, @"grid_2_2 should be assigned");}
{cc044}Tuesday, August 16, 2011
![Page 25: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/25.jpg)
Tuesday, August 16, 2011
![Page 26: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/26.jpg)
Verify where andWHY
your test failed
Tuesday, August 16, 2011
![Page 27: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/27.jpg)
"Understanding the View Management Cycle section of Apple's View
Controller Programming Guide for iOS"
Tuesday, August 16, 2011
![Page 28: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/28.jpg)
// TestGameViewController.m
- (void) setUp { [super setUp]; gameViewController_ = [[GameViewController alloc] init];
// trigger loading of nib before every test gameViewController_.view; }
{cc046}Tuesday, August 16, 2011
![Page 29: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/29.jpg)
More Tests for GameViewController
• cc047, cc048 - testClearView
• cc049, cc050, cc051 - testPlaceMarkInValidLocations
• cc052, cc053 - testPlaceMarkInInvalidLocations
Tuesday, August 16, 2011
![Page 30: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/30.jpg)
Test that we can display a message
GameViewController
Tuesday, August 16, 2011
![Page 31: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/31.jpg)
Why?
• The cost of doing so is very low
• It makes our class more testable
• It hides implementation details
• It makes our interface cleaner
Tuesday, August 16, 2011
![Page 32: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/32.jpg)
// TestGameViewController.m
- (void) testSetMessage { [gameViewController_ setMessage:@"New Message"]; GHAssertEqualStrings( gameViewController_.messageLabel.text, @"New Message", @"messageLabel.text should be 'New Message'");}
{cc054,cc055}Tuesday, August 16, 2011
![Page 33: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/33.jpg)
Testing components with dependencies
Tuesday, August 16, 2011
![Page 34: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/34.jpg)
Using Delegate Pattern and OCMock
to Test User Input
Tuesday, August 16, 2011
![Page 35: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/35.jpg)
Continuum of Testing Types
BrittlenessComplexityDuration
Intrusiveness
FrequencyEase of useImmediacy
Unit
Integration
GUI
Tuesday, August 16, 2011
![Page 36: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/36.jpg)
Adding a protocol and delegate to
GameViewController
Tuesday, August 16, 2011
![Page 37: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/37.jpg)
// GameViewController.h
@protocol GameViewUserInput- (void) userSelectedRow:(int) row col:(int) col;@end
@interface GameViewController : UIViewController { id< GameViewUserInput> delegate_;...
@property (nonatomic, assign) id<GameViewUserInput> delegate;
{cc056}Tuesday, August 16, 2011
![Page 38: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/38.jpg)
Using mock objects as stand-ins
Tuesday, August 16, 2011
![Page 39: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/39.jpg)
We want to avoid using other “real” classes in our tests
Tuesday, August 16, 2011
![Page 40: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/40.jpg)
Why avoid using other classes?
• It requires the other class to exist and and be at least partially functional
• It means working on multiple classes at the same time
• It increases complexity of our test setup
• It increases the brittleness of our tests
Tuesday, August 16, 2011
![Page 41: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/41.jpg)
Benefits of using substitutes
like OCMock?
• Reduces how much code we have to write
• Keeps us from worrying about whether the "real" class is behaving correctly,
• Focuses us on responsibilities of our class
• Reduces interdependencies of our tests
Tuesday, August 16, 2011
![Page 42: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/42.jpg)
Test that each grid button is linked to an
action
GameViewController
Tuesday, August 16, 2011
![Page 43: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/43.jpg)
// TestGameViewController.h
- (void) testActionsLinked { NSArray *actions = [gameViewController.grid_0_0 actionsForTarget:gameViewController forControlEvent:UIControlEventTouchUpInside]; GHAssertEquals([actions count], (NSUInteger) 1, @"should have one action for 'Touch Up Inside' for grid_0_0");}
{cc057,cc058}Tuesday, August 16, 2011
![Page 44: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/44.jpg)
// TestGameViewController.h
- (void) checkActionsFor:(UIButton *) button { NSArray *actions = [button actionsForTarget:gameViewController forControlEvent:UIControlEventTouchUpInside]; GHAssertEquals([actions count], (NSUInteger) 1, @"should have one action for 'Touch Up Inside' for grid_0_0");}
- (void) testActionsLinked { [self checkActionsFor:gameViewController.grid_0_0];... [self checkActionsFor:gameViewController.grid_2_2];}
{cc059,cc060}Tuesday, August 16, 2011
![Page 45: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/45.jpg)
Test that user input generates a message to the delegate method
when appropriate
GameViewController
Tuesday, August 16, 2011
![Page 46: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/46.jpg)
- (void) testGameViewUserInputDelegateMethodCalled { id mock = [OCMockObject mockForProtocol:@protocol(GameViewUserInput)]; // tell the mock to expect a method to be called [[mock expect] userSelectedRow:0 col:0]; // set the delegate on gameViewController_ so we// can know if it actually calls the delegate's method gameViewController_.delegate = mock; // trigger action for button grid_0_0 (row 0, col 0) [gameViewController_ gridButtonAction:gameViewController_.grid_0_0]; // ask the mock if all of our expectations were met [mock verify];}
{cc061}Tuesday, August 16, 2011
![Page 47: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/47.jpg)
Tuesday, August 16, 2011
![Page 48: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/48.jpg)
// GameViewController.m
- (void) handleUserSelectedRow:(int) row col:(int) col { [self.delegate userSelectedRow:row col:col];}
- (IBAction) gridButtonAction:(id) sender { if ([sender isEqual:self.grid_0_0]) { [self handleUserSelectedRow:0 col:0]; } else if ([sender isEqual:self.grid_0_1]) { [self handleUserSelectedRow:0 col:1];... } else if ([sender isEqual:self.grid_2_2]) { [self handleUserSelectedRow:2 col:2]; }}
{cc062}Tuesday, August 16, 2011
![Page 49: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/49.jpg)
- (void) checkUserInputForButton:(UIButton *) button expectRow:(int) row col:(int) col {... same as old simpler method}
- (void) testGameViewUserInputDelegateMethodCalled { [self checkUserInputForButton:gameViewController.grid_0_0 expectRow:0 col:0]; [self checkUserInputForButton:gameViewController.grid_2_2 expectRow:2 col:2];}
{cc063}Tuesday, August 16, 2011
![Page 50: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/50.jpg)
Lot’s more to do
Tuesday, August 16, 2011
![Page 51: Test Driven Development in the iOS World Part 2 · 2011. 8. 16. · Test Driven Development in the iOS World Part 2 Doug Sjoquist @dwsjoquist Tuesday, August 16, 2011](https://reader036.fdocuments.net/reader036/viewer/2022071420/61193cffb830946ecb44f3ff/html5/thumbnails/51.jpg)
TDD in iOSComing soon: TDD/iOS tutorial series on
http://www.raywenderlich.com
Doug Sjoquisthttp://www.sunetos.com
@dwsjoquist
Tuesday, August 16, 2011