objective-c – 为什么我的OCUnit测试失败了“代码138”?
我正在尝试使用XCode 3.1学习
objective-c.我一直在做一个小程序,决定添加单元测试.
我按照苹果开发者页面 – Automated Unit Testing with
试图隔离我的错误,我重新遵循上面的单元测试示例中的步骤,并且示例工作.当我添加了我的代码和测试用例的简化版本时,返回错误. 这是我创建的代码: 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",testCard.rank); [testCard release]; } @end 解决方法
我自己遇到过这么多次,而且总是很讨厌.基本上,这通常意味着您的单元测试会崩溃,但并不能帮助隔离错误.如果单元测试在崩溃之前生成输出(打开Build> Build Results),通常至少可以了解发生问题时运行的是什么测试,但这通常不是太有帮助.
跟踪原因的最好的一般建议是调试您的单元测试.使用OCUnit时,不幸的是选择Run>调试.但是,您正在使用的教程有一个附近的标题为“使用调试器与OCUnit”的部分,该部分解释了如何在Xcode中创建自定义可执行文件,以便调试器附加的方式执行单元测试.当你这样做时,调试器将停止错误发生的位置,而不是在火焰中下降时得到神秘的“代码138”. 虽然我可能无法猜到导致错误的原因,但我确实有一些建议… 从来没有,EVER会在init方法中自动释放自己 – 它违反了保留释放内存规则.如果对象意外释放,那将导致崩溃.例如,在testInitByIndex方法中,testCard会自动释放,因此,最后一行的[testCard release]可以保证崩溃. 以下是这些建议的测试文件可能是这样的: CardTest.m #import <SenTestingKit/SenTestingKit.h> #import "Card.h" @interface CardTest : SenTestCase @end @implementation CardTest - (void) testInitWithIndex { Card *testCard = [[Card alloc] initWithIndex:13]; STAssertNotNil(testCard,@"Card not created successfully"); STAssertEquals(testCard.rank,@"Unexpected card rank"); [testCard release]; } @end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |