我目前正在嘗試使用XCode 3.1來學習objective-c。我一直在研究一個小程序,並決定添加單元測試。爲什麼我的OCUnit測試失敗,顯示「code 138」?
我遵循Apple Developer頁面上的步驟 - Automated Unit Testing with Xcode 3 and Objective-C。當我添加我的第一個測試時,測試失敗時工作正常,但是當我更正測試時,構建失敗。 Xcode中報告了以下錯誤:
error: Test host '/Users/joe/Desktop/OCT/build/Debug/OCT.app/Contents/MacOS/OCT' exited abnormally with code 138 (it may have crashed).
試圖孤立我的錯誤,我再接着從上面的單元測試示例中的步驟和例子工作。當我添加我的代碼和測試用例的簡化版本時,返回錯誤。
這裏是我創建的代碼:
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 b = YES; NSLog(@「%@」,b);請注意,如果b = NO,它不會崩潰! – Rob 2011-06-08 06:20:35