Forked from aquarius/XCTestCase+MNAsynchronousTestCase.h
Created
February 6, 2017 13:27
-
-
Save Sumit1991Saha/df914cb31e3afc7714baafd97ee6429a to your computer and use it in GitHub Desktop.
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
Usage: | |
- (void)testCreateNewDocument | |
{ | |
[self mn_preformWithTimeout:10 asynchronousTest:^{ | |
[[MNDocumentController sharedDocumentController] createNewDocumentInFolder:nil withCompletionHandler:^(MNDocument *document, MNDocumentReference *reference) { | |
XCTAssertTrue(document && reference, @"Failed to create document."); | |
[self mn_finishRunningAsynchronousTest]; | |
}]; | |
}]; | |
} |
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
// | |
// XCTestCase+MNAsynchronousTestCase.h | |
// MindNodeTouch | |
// | |
// Created by Markus Müller-Simhofer on 28.07.13. | |
// Copyright (c) 2013 IdeasOnCanvas GmbH. All rights reserved. | |
// | |
#import <XCTest/XCTest.h> | |
@interface XCTestCase (MNAsynchronousTestCase) | |
- (void)mn_preformWithTimeout:(NSTimeInterval)timeout asynchronousTest:(void (^)(void))asynchronousTest; | |
- (void)mn_finishRunningAsynchronousTest; | |
@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
// | |
// XCTestCase+MNAsynchronousTestCase.m | |
// MindNodeTouch | |
// | |
// Created by Markus Müller-Simhofer on 28.07.13. | |
// Copyright (c) 2013 IdeasOnCanvas GmbH. All rights reserved. | |
// | |
#import "XCTestCase+MNAsynchronousTestCase.h" | |
#import <objc/runtime.h> | |
static char mn_waitingForAsynchronousTestKey; | |
@implementation XCTestCase (MNAsynchronousTestCase) | |
- (void)mn_preformWithTimeout:(NSTimeInterval)timeout asynchronousTest:(void (^)(void))asynchronousTest | |
{ | |
// run test block | |
[self mn_setWaitingForAsynchronousTest:YES]; | |
asynchronousTest(); | |
NSTimeInterval timeoutTime = [[NSDate dateWithTimeIntervalSinceNow:timeout] timeIntervalSinceReferenceDate]; | |
while ([self mn_waitingForAsynchronousTest]) { | |
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]]; | |
if ([NSDate timeIntervalSinceReferenceDate] > timeoutTime) { | |
XCTFail(@"Test timed out! Did you forget to call -mn_finishRunningAsynchronousTest"); | |
[self mn_setWaitingForAsynchronousTest:NO]; | |
} | |
} | |
} | |
- (void)mn_finishRunningAsynchronousTest | |
{ | |
[self mn_setWaitingForAsynchronousTest:NO]; | |
} | |
- (void)mn_setWaitingForAsynchronousTest:(BOOL)isWaiting; | |
{ | |
objc_setAssociatedObject(self, &mn_waitingForAsynchronousTestKey, @(isWaiting), OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (BOOL)mn_waitingForAsynchronousTest | |
{ | |
NSNumber *valueObject = objc_getAssociatedObject(self, &mn_waitingForAsynchronousTestKey); | |
return [valueObject boolValue]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment