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 hidden or 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 hidden or 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 hidden or 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 |
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jonah, first of all thank you for posting this. It was a huge help.
You might want to update the example_test.m to the following so that anyone who tries to implement your example will not run into issues with failing tests....
Please note the new name of the local variable that will receive the JSON response... apiResponse. This is to avoid an issue where
response = JSON;
was setting theNSHTTPURLResponse *response
which is a param in the success block of the AFJSONRequestOperation and ultimately leading to failing tests.