1

나는 블록을 인수로 취하는 방법을 테스트하기 위해 OCMockXCTest을 사용하고 있습니다. 블록이 성공과 실패 모두에서 실행되는지 테스트하고 싶습니다. 아래 코드는 블록이 실행되었는지 테스트하기에 충분합니까?블록이 OCMock으로 실행되는지 테스트

__block BOOL didExecute = NO; 

[testSubject performActionWithBlock:^(id result, NSError *error) { 
    didExecute = YES; 
}]; 

XCTAssertTrue(didExecute); 

답변

3

당신이 블록이 실행되었는지 확인하려면 은 엑스 코드 (6)에 가장 좋은 방법은 XCTestExpectation을 사용하는 것입니다

// Create an expectation with whatever description you want 
// The description will be displayed in the test report if the expectation causes the test to fail 
XCTestExpectation *blockExpectation = [self expectationWithDescription:@"Block Expectation"]; 

[testSubject performActionWithBlock:^(id result, NSError *error) { 
    // Inside the block, fulfill the expectation 
    [blockExpectation fulfill]; 
}]; 

// The timeout should be the max amount of time you want the test to wait 
[self waitForExpectationsWithTimeout:1.0 handler:nil]; 
+0

딱! 이것이 Xcode 6에 도입 된 것을 알지 못했습니다. – psobko