为什么在GHUnit中的异步测试中的false断言会导致应用程序崩溃,而不是仅仅失败测试? [英] Why does a false assertion in async test in GHUnit crash the app instead of just failing the test?

查看:150
本文介绍了为什么在GHUnit中的异步测试中的false断言会导致应用程序崩溃,而不是仅仅失败测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的查看次数非常少,目前没有答案。如果你有一个建议要改变这个问题,以获得更多的眼球,我会很高兴听到他们。欢呼!



我使用 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; //委托方法在主线程上调用。

[self.operationQueue addOperation:op];

[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}

- (void)didFinishAsyncOperation
{
GHAssertTrue(NO,@这应该触发一个失败的测试,但崩溃我的应用程序。

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

@end


解决方案>

我已经挖了一个星期找到一个解决方案,当我终于陷入了休息。这是一个有点奇怪的旁边没有对赏金问题的意见,没有人关心尝试答案。我认为这个问题可能是愚蠢的,但没有downvote和没有人关心纠正它。 StackOverflow是否饱和?



解决方案。



从回调方法,但把断言回原来的测试。 wait方法实际上是阻塞线程,我以前没有想过。如果您的异步回调接收到任何值,只需将它们存储在一个ivar或属性,然后在您的原始测试方法中基于它们断言。



这关心的断言不会导致任何崩溃。

   - (void)testAsyncOperation 
{
[self prepare];

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

op.delegate = self; //委托方法在主线程上调用。

[self.operationQueue addOperation:op];

//`waitfForStatus:timeout`方法会阻塞这个线程。
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];

//在回调完成后,它继续这里。
GHAssertTrue(NO,@这会触发一个失败的测试,没有任何崩溃。
}

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


This question has very few views and no answers yet. If you have a suggestion what to change about this question to get more eyeballs, I'd be happy to hear them. Cheers!

I'm using GHAsyncTestCase to test a custom NSOperation of mine. I'm setting the test case as a delegate on the operation object and I'm calling didFinishAsyncOperation on the main thread when it's done.

When an assertion fails it throws an exception, which ought to be caught by the test case to render the test as "failed". But instead of this expected behavior, my app get's aborted by Xcode as soon as the assertion fails.

* Terminating app due to uncaught exception 'GHTestFailureException', reason: ''NO' should be TRUE. This should trigger a failed test, but crashes my app instead.'

I'm obviously doing something wrong. Who can tell me?

@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

解决方案

I've been digging for a week to find a solution to this when I finally caught a break. It's been a bit weird having next to no views on a bounty question and no one cared to attempt an answer. I was thinking the question might be stupid, but there were no downvotes and no one cared to correct it either. Has StackOverflow become that saturated?

A solution.

The trick is to not assert anything from the callback method, but put the assertions back in the original test. The wait method is actually blocking the thread, which I didn't think of before. If your async callback receives any values, just store them in an ivar or property and then make assertions based on them in your original test method.

This takes care of the assertions not causing any crashes.

- (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)];
}

这篇关于为什么在GHUnit中的异步测试中的false断言会导致应用程序崩溃,而不是仅仅失败测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆