2016-05-17 5 views
0

私はAsyncタスクにボルトフレームワークを使用しています。どうすればcontinueWithBlockセクションにあるコードをテストできますか?OCMOCKテストブロック

BOOL wasFetchedFromCache; 
    [[store fetchFileAsync:manifestURL allowfetchingFromCache:YES fetchedFromCache:&wasFetchedFromCache] 
     continueWithBlock:^id(BFTask *task) { 
      NSData *fileContents = task.result; 
      NSError *localError; 

      // code to test 
      return nil 
     }]; 

答えて

0

非同期タスクをテストするには、XCTestExpectationを使用する必要があります。これにより、将来期待される期待を作成することができます。つまり、返された将来の結果はテストケースの期待値とみなされ、テストはアサートされた結果を受け取るまで待機します。私はシンプルな非同期テストを書く以下のコードを見てください。

- (void)testFetchFileAsync { 
    XCTestExpectation *expectation = [self expectationWithDescription:@"FetchFileAsync"]; 
    BOOL wasFetchedFromCache; 
    [[store fetchFileAsync:manifestURL allowfetchingFromCache:YES fetchedFromCache:&wasFetchedFromCache] 
    continueWithBlock:^id(BFTask *task) { 
     NSData *data = task.result; 
     NSError *localError; 

     XCTAssertNotNil(data, @"data should not be nil"); 

     [expectation fulfill]; 
     // code to test 
     return nil 
    }]; 

    [self waitForExpectationsWithTimeout:15.0 handler:^(NSError * _Nullable error) { 
     if (error) { 
      NSLog(@"Timeout error"); 
     } 
    }]; 

    XCTAssertTrue(wasFetchedFromCache, @"should be true"); 
} 
関連する問題