2016-08-11 3 views
4

APIを内部的に呼び出すメソッドがあります。このメソッドには完了ハンドラはありません。書き方補完なしの非同期メソッドのユニットテストObj Cのブロック

-(void) methodToBeTested{ 
    [self callAPIWithCompletionHandler:^(NSArray *data,NSError *error) 
    { 
     //Here I get the response and sets the models. 
    }]; 
} 

ここで、API呼び出し後に設定されたモデルのメソッド "methodToBeTested"をテストする必要があります。

何か提案がありますか?

+0

これは正直なところ、統合テストのようです。単体テストでは、通常、このメソッドをスタブし、他のテストコンポーネントを準備する必要がある場合はモックデータを返します。 – bplattenburg

答えて

1

例:のXcodeのドキュメントとテストの下で非同期操作のライティングテストで

XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:@"document open"]; 

    NSURL *URL = [[NSBundle bundleForClass:[self class]] 
           URLForResource:@"TestDocument" withExtension:@"mydoc"]; 
    UIDocument *doc = [[UIDocument alloc] initWithFileURL:URL]; 
    [doc openWithCompletionHandler:^(BOOL success) { 
     XCTAssert(success); 
     [documentOpenExpectation fulfill]; 
    }]; 

    [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) { 
     [doc closeWithCompletionHandler:nil]; 
    }]; 
} 

ルック。 https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html

関連する問題