1

Я использую OCMock и XCTest для проверки метода, который принимает блок в качестве аргумента. Я хочу проверить, что блок выполняется как по успеху, так и по ошибке. Будет ли код ниже достаточным для проверки того, что блок был выполнен?Тестирование, если блок выполняется с OCMock

__block BOOL didExecute = NO; 

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

XCTAssertTrue(didExecute); 

ответ

3

Если вы хотите, чтобы проверить, что блок был выполнен, лучший способ в Xcode 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

Смежные вопросы