When writing a certain asynchronous test using XCTest and XCTestExpectation I would like to assert that a certain block was not executed. The following code is successful in asserting that a block was executed and if not the test fails.
#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);
}
}];
}
There doesn't seem to be an obvious way to execute this the other way around; where the test succeeds if the block did not execute after the timeout and fails if it executed before the timeout. Adding to this, I would like to assert that the block executed at a later time when a different condition is met.
Is there a straightforward way to do this with XCTestExpectation or will I have to create a workaround?
You can achieve this with a
dispatch_aftercall that is scheduled to run just before your timeout. Use aBOOLto record if the block has executed, and an assertion to pass or fail the test once the expectation is completed.