2016-10-24 2 views
0

테스트하고 싶은 메소드 중 하나를 조롱하는 데 어려움을 겪고 있습니다. 메소드의 스텁 블록 OCMock

- (void) startFetching:(MyParameter *) parameter 
{ 
    self.fetcher = [[MyFetcher alloc] initWithContext:xxxx andObserver:nil]; 
    self.fetcher.parameters = @[parameter]; 
    [self.fetcher startWithCompleteionBlock:^(id<MyOperation> _Nonnull operation) { 
     if(operation.errors.count > 0) { 
      [self.delegate failedWithError:operation.errors.firstObject]; 
     } else{ 
      FetcherResponse *response = [MyFetcherResponse cast:operation]; 
      NSArray *array = response.responseArray; 
      if(array.count == 1) { 
       [self.delegate completedWithSuccess:array.firstObject]; 
      } 
     } 
    }]; 
} 

지금 내가 testStartFetching 같은 시험 방법을 가지고 있고이 방법을 테스트하려는처럼 내 코드가 어떻게 보이는지 아래

더 많거나 적은이다. 이 부분을 스텁 (stub) 할 수있는 방법을 모르겠다. 성공한 경우에는 적절한 배열을 반환하고 오류가 발생하면 오류를 반환하고 오류가있는 경우에는 failedWithError:operation이 호출되고 그렇지 않으면 completedWithSuccess이 호출된다.

저는 OCMock 프레임 워크를 객관적인 C 언어로 사용하고 있으며, 단위 테스트를 처음 접했습니다. 어떤 도움을 주시면 감사하겠습니다.

답변

0

operation (오류 있음)을 반환하는 완료 블록이있는 스텁 방식. 그런 다음 대리인의 메서드 - failedWithError에 올바른 매개 변수 (오류)가 있는지 확인합니다.

id<MyOperation> operation = [[MyClassOperaion alloc] init]; 
    NSError *error = [NSError new]; 
    operation.errors = @[error]; 

    OCMStub([self.fetcher startWithCompleteionBlock:([OCMArg checkWithBlock:^BOOL(void(^passedBlock)(id<MyOperation> _Nonnull operation)) { 
     passedBlock(operation); 
     return YES; 
    }])]); 

    OCMVerify([self.delegate failedWithError:error]); 
+0

감사합니다. 이 someAperation 무엇입니까? 그리고 통과 블록 (someOperation)은 무엇을합니까? – Madu

+0

@ 마두 나는 더 명확하게 설명하려고 노력했다. 뭔가 명확하지 않은지 알려주세요. –

+0

고마워요. 그것은 나를 많이 도왔다. – Madu