2015-01-20 49 views
1

我使用OCMockXCTest來測試一個將塊作爲參數的方法。我想測試塊是否在成功和失敗時執行。下面的代碼是否足以測試塊是否已被執行?測試一個塊是否使用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 2015-01-22 19:02:28