Port of schuchert.wikispaces.com


tdd.objectivec.XCodeProjectSetup

tdd.objectivec.XCodeProjectSetup

Why?

It took a bit of digging to come up with the steps required to get basic unit testing working in XCode. These steps borrow heavily from the following sites (in no order, because I don’t remember what all it took to get this working):

Much of my confusion comes from the difference between using “raw” Objective-C (relatively easy) versus XCode and all that implies. In any case, that’s the environment I’ll be using for courses, so that’s what I need to be able to get working.

What These Steps Describe

Technology:

These steps describe what you’ll need to do to set up a framework, not an application. If you want to set up an application, review either of the bottom two links in the Why section.

Video Version

Tdd Objective C;

Setting up Project

At this point, you should have a window that resembles the following:

Configuring for Unit Tests

Adding a Test Fixture

Now you’ll add a simple fixture with a failing test, then get the test to pass.

Create a Failing Test

#import <SenTestingKit/SenTestingKit.h>

@interface ANewlyCreatedRpnCalculatorShould : SenTestCase {
}

@end
#import "ANewlyCreatedRpnCalculatorShould.h"

@implementation ANewlyCreatedRpnCalculatorShould

-(void)testWillFail {
	STAssertTrue(0, @"");
}

@end

Build to run the test

Let’s assume the for building you’ll want to run the tests every time.

Get the test to pass

	STAssertTrue(0, @"");

To:

	STAssertTrue(1, @"");

Really Getting it all Tied Together

Now it’s time to get the RpnCalculator framework linked into the test target.

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

@implementation ANewlyCreatedRpnCalculatorShould

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

@end

This example imports a type that does not yet exist. So add it:

#import <Cocoa/Cocoa.h>

@interface RpnCalculator : NSObject {
	int accumulator;
}

@property (readonly) int accumulator;

@end
#import "RpnCalculator.h"

@implementation RpnCalculator

@synthesize accumulator;

@end

If you build now, you will get a linking error. Try it:

To Fix this:

Congratulations

You have a project configured to run unit tests when built.


Comments

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