ios - xcodebuild 无法使用 XCTestExpectation 运行异步测试?

标签 ios objective-c xcode xctestexpectation

我有三个异步测试。在 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/

相关文章:

iOS UIBarButtons 不设置由 UIActionSheet iOS 6.1 启用

iphone - 用varargs编写replaceTokensWithStrings:方法

ios - Xcode中是否有CarPlay模拟器

ios - 在界面生成器中使用现有的自定义 UIView

ios - 未找到架构 armv7 的 React Native 符号

ios - 我们如何在IOS中将标签栏放在屏幕顶部

objective-c - iOS 面朝上方向问题

ios - 对象的潜在泄漏-调用函数“ABMultiValueCopyValueAtIndex”将返回保留计数为+1的Core Foundation对象

ios - 是否可以为 UITableView 中的每个部分设置不同的分隔符颜色?

iOS:动画图标模型 "springboard"