gpt4 book ai didi

xctest - XCTestExpectation:如何避免在等待上下文结束后调用执行方法?

转载 作者:行者123 更新时间:2023-12-03 11:31:55 25 4
gpt4 key购买 nike

我正在使用Xcode 6的新异步测试功能。当异步任务在超时之前结束时,一切工作正常。但是,如果任务花费的时间超过超时时间,事情就会变得更加复杂。

这是我进行测试的方式:

@interface AsyncTestCase : XCTestCase @end

@implementation AsyncTestCase

// The asynchronous task would obviously be more complex in a real world scenario.
- (void) startAsynchronousTaskWithDuration:(NSTimeInterval)duration completionHandler:(void (^)(id result, NSError *error))completionHandler
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
completionHandler([NSObject new], nil);
});
}

- (void) test1TaskLongerThanTimeout
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Test 1: task longer than timeout"];
[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
}

- (void) test2TaskShorterThanTimeout
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Test 2: task shorter than timeout"];
[self startAsynchronousTaskWithDuration:5 completionHandler:^(id result, NSError *error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}

@end

不幸的是,在超时到期后调用 fulfill方法会使测试套件崩溃,并显示以下错误:

API violation - called -[XCTestExpectation fulfill] after the wait context has ended.


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API violation - called -[XCTestExpectation fulfill] after the wait context has ended.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c3a6f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a760bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010c3a6d9a +[NSException raise:format:arguments:] + 106
3 Foundation 0x000000010a37d5df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 XCTest 0x0000000115c48ee1 -[XCTestExpectation fulfill] + 264
...
)
libc++abi.dylib: terminating with uncaught exception of type NSException

当然,我可以在调用 fulfill方法之前检查测试是否完成:

- (void) test1TaskLongerThanTimeout
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Test 1: task longer than timeout"];

__block BOOL testIsFinished = NO;
[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
if (testIsFinished) {
return;
}
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:2 handler:^(NSError *error) {
testIsFinished = YES;
}];
}

但这似乎过于复杂,并且使测试更难阅读。我想念什么吗?有没有更简单的方法来解决这个问题?

最佳答案

是的,有一种更简单的方法来避免此API违规问题:只需将您的期望变量声明为__weak即可。尽管没有明确记录,但期望将在超时到期时释放。因此,如果任务花费的时间超过超时时间,则在调用任务完成处理程序时,期望变量将为nil。因此,fulfill方法将在nil上被调用,什么也不做。

- (void) test1TaskLongerThanTimeout
{
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Test 1: task longer than timeout"];
[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
}

关于xctest - XCTestExpectation:如何避免在等待上下文结束后调用执行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27555499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com