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

예 : 엑스 코드 문서와 테스트에서 비동기 작업의 쓰기 시험에서

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