为设置和拆除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,然后tear down将被调用。它在安装前拆卸,有谁能告诉我这个问题,为什么它在安装之前就被拆除了。

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

您可以通过使用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(@"nnCALL STACK: %@nn",exception.callStackSymbols);
    }
} 
@end

最新更新