这个问题的观点很少,也没有答案。如果您有关于如何改变这个问题以吸引更多眼球的建议,我很乐意听到。干杯!
我在用着GHAsyncTestCase https://github.com/gabriel/gh-unit/blob/master/Classes/GHAsyncTestCase.m测试自定义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
当我终于休息一下时,我已经挖掘了一个星期来找到解决方案。对赏金问题几乎没有任何看法,而且没有人愿意尝试回答,这有点奇怪。我认为这个问题可能很愚蠢,但没有人投反对票,也没有人愿意纠正它。 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)];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)