Port of schuchert.wikispaces.com


tdd.objectivec.ExtendProjectByAddingTests

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:

-(void)testAllowItsAccumulatorToBeSet {
	RpnCalculator *calculator = [[RpnCalculator alloc] init];
	calculator.accumulator = 42;
	STAssertEquals(42, calculator.accumulator, @"");
	[calculator release];
}

This test fails because the accumulator property is currently read only. Remove that restriction by updating RpnCalculator.h from:

@property (readonly) int accumulator;

To:

@property int accumulator;

First Refactoring: Clean up duplication

In each of the tests, the code:

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

#import <SenTestingKit/SenTestingKit.h>
#import "RpnCalculator.h"

@interface ANewlyCreatedRpnCalculatorShould : SenTestCase {
	RpnCalculator *calculator;
}

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

@end

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.

#import "ANewlyCreatedRpnCalculatorShould.h"
#import "RpnCalculator.h"

@implementation ANewlyCreatedRpnCalculatorShould

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

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

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

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:

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

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

Everything is back to green.

Supporting basic math operations: addition


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.