【发布时间】:2009-07-09 11:21:00
【问题描述】:
我目前正在尝试使用 XCode 3.1 学习 Objective-c。我一直在开发一个小程序,并决定为其添加单元测试。
我按照 Apple 开发者页面 - Automated Unit Testing with Xcode 3 and Objective-C 上的步骤操作。当我添加我的第一个测试时,当测试失败时它运行良好,但是当我纠正测试时构建失败。 Xcode报如下错误:
错误:测试主机“/Users/joe/Desktop/OCT/build/Debug/OCT.app/Contents/MacOS/OCT”异常退出,代码为 138(它可能已崩溃)。
为了找出我的错误,我重新按照上面单元测试示例中的步骤进行操作,并且示例成功了。当我添加代码的简化版本和测试用例时,错误返回。
这是我创建的代码:
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
【问题讨论】:
-
仅供参考,我在测试中将 BOOL 记录为字符串时遇到了同样的错误:BOOL b = YES; NSLog(@"%@", b);请注意,如果 b = NO,它不会崩溃!
标签: objective-c unit-testing xcode memory-management ocunit