我有三个异步测试。在 Xcode 中测试时一切都运行良好,但测试用例无法使用 xcodebuild 构建。我收到 11 个构建错误,全部与 XCTestExpectation 相关。
示例:
error: unknown type name 'XCTestExpectation' @property XCTestExpectation *expectationNoImage;
我正在使用最新的命令行工具(Xcode 6.1.1)。 xcodebuild -version 正确地说明了这一点。
我正在使用以下命令运行构建
xcodebuild -project myprojasync.xcodeproj -scheme testScheme -configuration Debug -sdk iphonesimulator7.1 clean test | ocunit2junit
如果我注释掉异步测试及其对应项,则所有内容都可以使用相同的命令完美运行。
编辑:这是测试方法之一。
@property XCTestExpectation *expectationPass;
-(void)testTaskPass{
//Expectation
self.expectationPass = [self expectationWithDescription:@"Testing Async Works"];
[self.asyncTask getInfo]; //asynchronous call
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
-(void)returnedFrom:(NSURL *)url with:(UIImage *)image{
if([[url absoluteString] isEqualToString: @"http://correcturl.com"]){
[self.expectationPass fulfill];
}
}
最佳答案
尚不清楚您何时满足期望。我看到你有:
-(void)returnedFrom:(NSURL *)url with:(UIImage *)image;
那里有一个 fulfill,但我不清楚你何时调用它。
我会这样做:
@property XCTestExpectation *expectationPass;
-(void)testTaskPass{
//Expectation
self.expectationPass = [self expectationWithDescription:@"Testing Async Works"];
//asynchronous call
[self.asyncTask getInfo:^(){
// some Assertions here...
[self.expectationPass fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error){
XCTAssertNil(error, "Error");
}];
}
关于ios - xcodebuild 无法使用 XCTestExpectation 运行异步测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28722894/