【问题标题】:Exception raised for the setup and teardown OCUnit test case设置和拆卸 OCUnit 测试用例引发异常
【发布时间】:2013-02-27 23:58:24
【问题描述】:
- (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。它在安装前的拆解, 任何人都在这个问题上建议我为什么在设置之前拆解。

【问题讨论】:

    标签: iphone ocunit


    【解决方案1】:

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

    【讨论】:

    • 对于我的 setup 和 tearDown 不应该抛出异常!我让 STFail 知道 [Problem setupProblem];已成功完成并且还用于拆卸 [Problem teardownproblem];如果此方法未正确设置拆卸,它会给我不需要抛出异常的错误值!
    【解决方案2】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      相关资源
      最近更新 更多