在对类进行单元测试时,我遇到了一个问题。当运行我的测试时,它编译时没有任何错误,但随后它崩溃了(它不会因为不满足断言而失败),并显示以下错误消息:
/Developer/Tools/RunPlatformUnitTests.include:451:0 Test rig '/Developer/Platforms
/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/Developer/usr/bin/otest'
exited abnormally with code 134 (it may have crashed).下面是我的代码:
类的接口:
@interface AbstractActionModel : NSObject
{
NSString* mName;
ActionType mType; // enum
float mDuration;
float mRepeatCount;
float mDelay;
NSArray* mTriggerAreas;
} 实现:
- (void) dealloc
{
[mTriggerAreas release];
[super dealloc];
}
- (id) initWithConfigData: (NSDictionary*) theConfigData
{
NSAssert(nil != theConfigData, @"theConfigData cannot be nil");
self = [super init];
if (self)
{
self.name = [theConfigData objectForKey:ACTION_NAME];
self.type = [[theConfigData objectForKey:ACTION_TYPE] intValue];
self.duration = [[theConfigData objectForKey:ACTION_DURATION] floatValue];
self.delay = [[theConfigData objectForKey:ACTION_DELAY] floatValue];
self.repeatCount = [[theConfigData objectForKey:ACTION_REPEAT_COUNT] floatValue];
self.triggerAreas = [theConfigData objectForKey:ACTION_TRIGGER_AREAS];
}
return self;
}下面是测试代码:
- (void) testCreateAction
{
SoundActionModel* testSoundAction = (SoundActionModel*)[SoundActionModelFactory createActionModel:self.actionConfig];
STAssertNotNil(testSoundAction, @"returned object must not be nil");
}工厂的createActionModel:方法:
+ (AbstractActionModel*) createActionModel:(NSDictionary *)config
{
NSAssert(config != nil, @"config must not be nil");
SoundActionModel* retVal = [[[SoundActionModel alloc] initWithConfigData:config] autorelease];
return retVal;
}如前所述:代码会编译,并在testCreateAction被注释掉时运行。问题似乎不是测试本身(即它的断言)。
从这些帖子(similar problem 1,similar problem 2)可以看出,这似乎是XCode中的一个错误,但这些链接指出了在使用核心数据(我不知道)或OCMock (我也不知道-至少不知道)时出现的问题。
有人能告诉我如何解决这类问题吗?如果它被证明是一个bug,我们将非常感谢你的解决办法。
发布于 2012-08-21 16:43:25
在开始使用OCUnit时,我也遇到了这个问题。这是由于尝试作为在逻辑测试模式而不是应用程序测试模式下设置的测试执行。如果测试中的代码对Cocoa或Cocoa Touch有一些依赖,则必须使用为应用程序测试设置的目标来运行此代码。
在我看来,测试运行器本身崩溃的事实看起来像是一个xcode bug,因为AppCode将继续通过这一点。
设置这些测试的一个很好的来源是here
https://stackoverflow.com/questions/5032707
复制相似问题