現在、XCode 3.1 を使用して Objective-C を学習しようとしています。私は小さなプログラムに取り組んでおり、それに単体テストを追加することにしました。
Apple Developer ページの手順に従いました - Automated Unit Testing with Xcode 3 and 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