我目前正在尝试使用 XCode 3.1 学习objective-c。我一直在开发一个小程序,并决定为其添加单元测试。
我按照 Apple Developer 页面上的步骤进行操作 -使用 Xcode 3 和 Objective-C 进行自动化单元测试。当我添加我的第一个测试时,当测试失败时它运行良好,但是当我纠正测试时构建失败。Xcode 报如下错误:
错误:测试主机“/Users/joe/Desktop/OCT/build/Debug/OCT.app/Contents/MacOS/OCT”异常退出,代码为 138(它可能已崩溃)。
为了找出我的错误,我重新按照上面单元测试示例中的步骤操作,并且示例有效。当我添加代码的简化版本和测试用例时,错误返回。
这是我创建的代码:
卡片.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
卡.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