Skip to content

Instantly share code, notes, and snippets.

@kwylez
Last active December 18, 2015 05:19
Show Gist options
  • Save kwylez/5731369 to your computer and use it in GitHub Desktop.
Save kwylez/5731369 to your computer and use it in GitHub Desktop.
quick example of using block property to handle async unit tests for iOS. This assumes you're using SenTest
@property (nonatomic, copy) void (^testForCompletionBlock)(BOOL *testCompleted);
- (void)setUp {
[super setUp];
_testForCompletionBlock = ^(BOOL *testCompleted) {
while (!*testCompleted) {
NSDate *cycle = [NSDate dateWithTimeIntervalSinceNow:0.01];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:cycle];
}
};
}
- (void)test_Delete_All_Bar_Objects {
@try {
__block BOOL testCompleted = NO;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bar" inManagedObjectContext:_sm_open_Context];
[fetchRequest setEntity:entity];
[_sm_open_Context executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) {
for (id obj in results) {
[_sm_open_Context deleteObject:obj];
[_sm_open_Context saveOnSuccess:^{
} onFailure:^(NSError *error) {
STAssertNil(error, @"There was an error! %@", error);
}];
}
testCompleted = YES;
} onFailure:^(NSError *error) {
testCompleted = YES;
STAssertNil(error, @"Error fetching! %@", error);
}];
_testForCompletionBlock(&testCompleted);
} @catch (NSException * e) {
NSLog(@"test_Delete_All_Bar_Objects failed: %@", [e description]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment