Attualmente sto cercando di imparare Objective-C utilizzando XCode 3.1. Ho lavorato su un piccolo programma e ho deciso di aggiungere test delle unità.Perché i miei test OCUnit falliscono con "codice 138"?
ho seguito le istruzioni riportate nella pagina di Apple Developer - Automated Unit Testing with Xcode 3 and Objective-C. Quando ho aggiunto il mio primo test, ha funzionato correttamente quando i test hanno avuto esito negativo, ma quando ho corretto i test la build non è riuscita. Xcode ha riportato il seguente errore:
error: Test host '/Users/joe/Desktop/OCT/build/Debug/OCT.app/Contents/MacOS/OCT' exited abnormally with code 138 (it may have crashed).
cercando di isolare il mio errore, ho ri-seguito i passi da esempio Unità prova di cui sopra e l'esempio funzionato. Quando ho aggiunto una versione semplificata del mio codice e un caso di test, l'errore è stato restituito.
ecco il codice che ho creato:
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
FYI ho ottenuto lo stesso errore di registrazione di un BOOL come una stringa nel mio test: BOOL b = SÌ; NSLog (@ "% @", b); Nota che se b = NO, non si blocca! – Rob