为什么GHUnit中的异步测试中的错误断言会使应用程序崩溃而不是仅仅失败测试?

时间:2011-09-30 17:11:47

标签: objective-c cocoa-touch cocoa unit-testing gh-unit

这个问题的观点很少,也没有答案。如果你有什么建议要改变这个问题以获得更多的眼球,我会很高兴听到它们。干杯!

我正在使用GHAsyncTestCase来测试我的自定义NSOperation。我将测试用例设置为操作对象的委托,并且当它完成时我在主线程上调用didFinishAsyncOperation

当断言失败时,它会抛出一个异常,应该被测试用例捕获,以使测试“失败”。但是,一旦断言失败,我的应用程序就会被Xcode中止,而不是这种预期的行为。

  

***由于未捕获的异常'GHTestFailureException'终止应用,原因:''NO'应该为TRUE。这应该会触发测试失败,但会导致我的应用崩溃。'

我显然做错了什么。谁能告诉我?

@interface TestServiceAPI : GHAsyncTestCase
@end

@implementation TestServiceAPI

    - (BOOL)shouldRunOnMainThread
    {
        return YES;
    }

    - (void)testAsyncOperation
    {
        [self prepare];

        MyOperation *op = [[[MyOperation alloc] init] autorelease];

        op.delegate = self; // delegate method is called on the main thread.

        [self.operationQueue addOperation:op];

        [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
    }

    - (void)didFinishAsyncOperation
    {
        GHAssertTrue(NO, @"This should trigger a failed test, but crashes my app instead.");

        [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testAsyncOperation)];
    }

@end

4 个答案:

答案 0 :(得分:12)

当我终于休息一下时,我已经挖了一个星期才找到解决方案。在赏金问题上没有任何意见,并且没有人愿意尝试回答,这有点奇怪。我当时认为这个问题可能很愚蠢,但没有任何支持,也没有人愿意纠正它。 StackOverflow会变得饱和吗?

解决方案。

诀窍是不要从回调方法断言任何东西,而是将断言放回原始测试中。 wait方法实际上阻塞了线程,我以前没有想到过。如果您的异步回调接收到任何值,只需将它们存储在ivar或属性中,然后在原始测试方法中根据它们进行断言。

这会处理不会造成任何崩溃的断言。

- (void)testAsyncOperation
{
    [self prepare];

    MyOperation *op = [[[MyOperation alloc] init] autorelease];

    op.delegate = self; // delegate method is called on the main thread.

    [self.operationQueue addOperation:op];

    // The `waitfForStatus:timeout` method will block this thread.
    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];

    // And after the callback finishes, it continues here.
    GHAssertTrue(NO, @"This triggers a failed test without anything crashing.");
}

- (void)didFinishAsyncOperation
{
    [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testAsyncOperation)];
}

答案 1 :(得分:2)

查找Xcode Breakpoints导航器,删除所有异常断点,这就是全部!

答案 2 :(得分:0)

查看GHUnit的头文件,看起来可能是您的代码应该发生的事情。 GHUnit的子类可以覆盖此方法:

// Override any exceptions; By default exceptions are raised, causing a test failure
- (void)failWithException:(NSException *)exception { }

不抛出异常,但更简单的解决方案是使用GHAssertTrueNoThrow而不是GHAssertTrue宏。

答案 3 :(得分:0)

我认为这个问题应该是“如何用GHUnit中的块测试方法”?

答案可以在这里找到:http://samwize.com/2012/11/25/create-async-test-with-ghunit/

相关问题