设置和拆卸OCUnit测试用例引发异常

时间:2013-02-27 23:58:25

标签: iphone ocunit

- (void)setUp
{
    [super setUp];
    @try {
        [Problem setupProblem];
    }
    @catch (NSException *exception) {

        NSLog(@"exception Caught %@: %@", [exception name], [exception reason]);
        STFail(@"Should Pass, no exception is expected. Exception <%@>", exception);
    }
}

- (void)tearDown
{
    // Tear-down code here.

    @try {
        [Problem teardownproblem];
    }
    @catch (NSException* exception) {

        NSLog(@"exception Caught %@: %@", [exception name], [exception reason]);
    STFail(@"Should Pass, no exception is expected. Exception <%@>", exception);
}
    }
-(void)testGetComponentNil{

    id testComponet = (id<Solution>)[Problem getComponent:nil];
            STAssertNil(testComponet, @"Return Nil");
STAssertNotNil(id<Solution>[problem getComponent:@"Problem"], @"");

}


exception Caught NSInternalInconsistencyException: Cannot teardownProblem() before setupProblem()

 <Cannot teardownProblem() before setupProblem().>

已经为我的信息首先调用了setup方法并调用了testcaseMethod然后将被调用。在安装之前拆卸, 任何人都建议我在这个问题上为什么在设置之前拆解它。

2 个答案:

答案 0 :(得分:0)

不要在setUp或tearDown中放置STFail断言。你也不需要处理异常; OCUnit将捕获并报告抛出的任何异常。

答案 1 :(得分:0)

您可以打印调用堆栈,并通常使用SenTestCaseDidFailNotification

查看这些异常

示例:

@implementation TestCase
- (void)setUp
{
    [super setUp];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFailTest:) name:SenTestCaseDidFailNotification object:nil];
}

- (void)didFailTest:(NSNotification *)notification
{
    SenTestCaseRun *theRun = notification.object;

    for (NSException *exception in theRun.exceptions) {
        NSLog(@"Exception: %@ - %@", exception.name, exception.reason);
        NSLog(@"\n\nCALL STACK: %@\n\n",exception.callStackSymbols);
    }
} 
@end