当使用 XCTest 和 XCTestExpectation 编写某个异步测试时,我想断言某个块没有被执行。以下代码成功断言块已执行,否则测试失败。
#import <XCTest/XCTest.h>
#import "Example.h"
@interface Example_Test : XCTestCase
@property (nonatomic) Example *example;
@end
@implementation Example_Test
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
- (void)testExampleWithCompletion {
self.example = [[Example alloc] init];
XCTestExpectation *expectation = [self expectationWithDescription:@"expection needs to be fulfilled"];
[self.example exampleWithCompletion:^{
[expectation fulfill]
}];
[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}
}];
}
似乎没有明显的方法可以反过来执行此操作;如果块在超时后未执行,则测试成功,如果在超时之前执行,则测试失败。除此之外,我想断言该块在满足不同条件时稍后执行。
是否有一种直接的方法可以使用 XCTestExpectation 执行此操作,或者我必须创建一个解决方法?