11

这个问题的观点很少,还没有答案。如果您有关于如何改变这个问题以获得更多眼球的建议,我很高兴听到他们的声音。干杯!

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

4 回答 4

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)];
}
于 2011-10-09T13:39:44.837 回答
2

查找您的 Xcode Breakpoints 导航器,删除所有异常断点,仅此而已!!!

于 2013-03-29T03:19:41.153 回答
0

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

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

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

于 2011-10-09T13:47:02.667 回答
0

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

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

于 2014-02-19T17:14:00.823 回答