测试块是否使用 OCMock 执行
Testing if a block is executed with OCMock
我正在使用 OCMock
和 XCTest
来测试将块作为参数的方法。我想测试块是否在成功和失败时执行。下面的代码是否足以测试该块是否已执行?
__block BOOL didExecute = NO;
[testSubject performActionWithBlock:^(id result, NSError *error) {
didExecute = YES;
}];
XCTAssertTrue(didExecute);
如果要检查一个块是否已经执行,Xcode6中最好的方法是使用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];
我正在使用 OCMock
和 XCTest
来测试将块作为参数的方法。我想测试块是否在成功和失败时执行。下面的代码是否足以测试该块是否已执行?
__block BOOL didExecute = NO;
[testSubject performActionWithBlock:^(id result, NSError *error) {
didExecute = YES;
}];
XCTAssertTrue(didExecute);
如果要检查一个块是否已经执行,Xcode6中最好的方法是使用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];