Last active
December 10, 2015 04:18
-
-
Save delebedev/4380377 to your computer and use it in GitHub Desktop.
unit testing of completion blocks
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
it (@"should be an awesome test of completion blocks", ^{ | |
NSString *key = [NSString UUID]; | |
[_apiClient playersWithIdentifiers:@[@11549892] fields:nil completion:^(NSArray *players) { | |
[[theValue([players count]) should] equal:@1]; | |
//some other logic | |
[[TestSemaphor sharedInstance] lift:key]; | |
} error:^(NSError *error) { | |
[[error should] beNil]; | |
[[TestSemaphor sharedInstance] lift:key]; | |
}]; | |
[[theValue([[TestSemaphor sharedInstance] waitForKey:key]) should] beTrue]; | |
}); |
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
// | |
// TestSemaphor.h | |
// BillsApp | |
// | |
// Created by Marin Todorov on 17/01/2012. | |
// Copyright (c) 2012 Marin Todorov. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface TestSemaphor : NSObject | |
@property (strong, atomic) NSMutableDictionary* flags; | |
+ (TestSemaphor *)sharedInstance; | |
- (BOOL)isLifted:(NSString*)key; | |
- (void)lift:(NSString*)key; | |
- (BOOL)waitForKey:(NSString*)key; | |
- (BOOL)waitForKey:(NSString *)key timeout:(NSTimeInterval)timeout; | |
@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
// | |
// TestSemaphor.m | |
// BillsApp | |
// | |
// Created by Marin Todorov on 17/01/2012. | |
// Copyright (c) 2012 Marin Todorov. All rights reserved. | |
// | |
#import "TestSemaphor.h" | |
@implementation TestSemaphor | |
@synthesize flags; | |
+(TestSemaphor *)sharedInstance { | |
static TestSemaphor *sharedInstance = nil; | |
static dispatch_once_t once; | |
dispatch_once(&once, ^{ | |
sharedInstance = [TestSemaphor alloc]; | |
sharedInstance = [sharedInstance init]; | |
}); | |
return sharedInstance; | |
} | |
- (id)init { | |
self = [super init]; | |
if (self != nil) { | |
self.flags = [NSMutableDictionary dictionary]; | |
} | |
return self; | |
} | |
- (void)dealloc { | |
self.flags = nil; | |
} | |
- (BOOL)isLifted:(NSString*)key { | |
return [self.flags objectForKey:key] != nil; | |
} | |
- (void)lift:(NSString*)key { | |
[self.flags setObject:@"YES" forKey:key]; | |
} | |
- (BOOL)waitForKey:(NSString *)key timeout:(NSTimeInterval)timeout { | |
BOOL keepRunning; | |
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout]; | |
do { | |
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate]; | |
keepRunning = ![[TestSemaphor sharedInstance] isLifted:key]; | |
if([timeoutDate timeIntervalSinceNow] < 0.0) { | |
[self lift:key]; | |
return NO; | |
} | |
} while (keepRunning); | |
return YES; | |
} | |
- (BOOL)waitForKey:(NSString*)key { | |
return [self waitForKey:key timeout:20.0]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment