tdd.objectivec.ExtendProjectByAddingTests

Background
This material assumes you have already created an XCode project with these steps.

Continue with next test
The next test adds the ability to set the accumulator's value: code format="c" -(void)testAllowItsAccumulatorToBeSet { RpnCalculator *calculator = RpnCalculator alloc] init];	calculator.accumulator = 42;	STAssertEquals(42, calculator.accumulator, @"");	[calculator release]; } [[code

This test fails because the accumulator property is currently read only. Remove that restriction by updating RpnCalculator.h from: code format="c" @property (readonly) int accumulator; code

To: code format="c" @property int accumulator; code

First Refactoring: Clean up duplication
In each of the tests, the code:
 * Allocates and initializes an RpnCalculator instance
 * Does some "real" work
 * Validates the results
 * Releases the memory allocated.

The first and last step are common, so we'll refactor the code using the Test Class as a full-fledged test fixture.

code format="c"
 * Update ANewlyCreatedRpnCalculatorShould.h:
 * 1) import 
 * 2) import "RpnCalculator.h"

@interface ANewlyCreatedRpnCalculatorShould : SenTestCase { RpnCalculator *calculator; }

-(void)setUp; -(void)tearDown;

@end code

This adds an instance variable that will be available to each test method. It also declares that the unit testing's setUp and tearDown hook methods will be used.

code format="c"
 * Now, update ANewlyCreatedRpnCalculatorShould.m:
 * 1) import "ANewlyCreatedRpnCalculatorShould.h"
 * 2) import "RpnCalculator.h"

@implementation ANewlyCreatedRpnCalculatorShould

-(void)setUp { calculator = [[RpnCalculator alloc] init]; }

-(void)tearDown { [calculator release]; } @end code

This adds support to remove common code, but we have not yet changed any of the unit tests.


 * Build your system.

Notice that the tests actually pass, but there are several warnings. The problem is that there is a local variable called calculator that hided the instance variable. While this "works" it's bad form. Since we now have support for common setup and tear down, simply update both of the unit tests:

code format="c" -(void)testHave0InItsAccumulator { STAssertEquals(0, calculator.accumulator, @""); }

-(void)testAllowItsAccumulatorToBeSet { calculator.accumulator = 42; STAssertEquals(42, calculator.accumulator, @""); } code


 * Build your system again.

Everything is back to green.