I'm currently trying to learn objective-c using XCode 3.1. I've been working on a small program and decided to add unit testing to it.
I followed the steps on the Apple Developer page - Automated Unit Testing with Xcode 3 and Objective-C. When I added my first test, it worked fine when the tests failed, but when I corrected the tests the build failed. Xcode reported the following error:
error: Test host '/Users/joe/Desktop/OCT/build/Debug/OCT.app/Contents/MacOS/OCT' exited abnormally with code 138 (it may have crashed).
Trying to isolate my error, I re-followed the steps from the Unit Test example above and the example worked. When I added a simplified version of my code and a test case, the error returned.
Here is the code I created:
Card.h
#import <Cocoa/Cocoa.h>
#import "CardConstants.h"
@interface Card : NSObject {
int rank;
int suit;
BOOL wild ;
}
@property int rank;
@property int suit;
@property BOOL wild;
- (id) initByIndex:(int) i;
@end
Card.m
#import "Card.h"
@implementation Card
@synthesize rank;
@synthesize suit;
@synthesize wild;
- (id) init {
if (self = [super init]) {
rank = JOKER;
suit = JOKER;
wild = false;
}
return [self autorelease];
}
- (id) initByIndex:(int) i {
if (self = [super init]) {
if (i > 51 || i < 0) {
rank = suit = JOKER;
} else {
rank = i % 13;
suit = i / 13;
}
wild = false;
}
return [self autorelease];
}
- (void) dealloc {
NSLog(@"Deallocing card");
[super dealloc];
}
@end
CardTestCases.h
#import <SenTestingKit/SenTestingKit.h>
@interface CardTestCases : SenTestCase {
}
- (void) testInitByIndex;
@end
CardTestCases.m
#import "CardTestCases.h"
#import "Card.h"
@implementation CardTestCases
- (void) testInitByIndex {
Card *testCard = [[Card alloc] initByIndex:13];
STAssertNotNil(testCard, @"Card not created successfully");
STAssertTrue(testCard.rank == 0,
@"Expected Rank:%d Created Rank:%d", 0, testCard.rank);
[testCard release];
}
@end