Last active
October 7, 2015 01:38
-
-
Save jonah-williams/3085287 to your computer and use it in GitHub Desktop.
Delay execution of Kiwi specs until an asynchronous process finishes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Kiwi/Kiwi.h> | |
#import "KWSpec+WaitFor.h" | |
SPEC_BEGIN(Example) | |
describe(@"it takes a while", ^{ | |
__block NSDictionary *apiResponse = nil; | |
beforeAll(^{ | |
__block BOOL requestCompleted = NO; | |
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request | |
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { | |
requestCompleted = YES; | |
apiResponse = JSON; | |
} | |
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { | |
requestCompleted = YES; | |
} | |
]; | |
[operation start]; | |
[KWSpec waitWithTimeout:3.0 forCondition:^BOOL() { | |
return requestCompleted; | |
}]; | |
}); | |
it(@"includes the related objects in the response", ^{ | |
[[[apiResponse objectForKey:@"children"] should] containObjects:@"foo", @"bar", @"baz", nil]; | |
}); | |
}); | |
SPEC_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "KWSpec.h" | |
@interface KWSpec (WaitFor) | |
+ (void) waitWithTimeout:(NSTimeInterval)timeout forCondition:(BOOL(^)())conditionalBlock; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "KWSpec+WaitFor.h" | |
@implementation KWSpec (WaitFor) | |
+ (void) waitWithTimeout:(NSTimeInterval)timeout forCondition:(BOOL(^)())conditionalBlock { | |
NSDate *timeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:timeout]; | |
while (conditionalBlock() == NO) { | |
if ([timeoutDate timeIntervalSinceDate:[NSDate date]] < 0) { | |
return; | |
} | |
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@atljeremy thanks for catching that. Sorry I gave you a poor example. I've applied your change so hopefully you've saved everyone else from similar confusion.