Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Sumit1991Saha/df914cb31e3afc7714baafd97ee6429a to your computer and use it in GitHub Desktop.
Save Sumit1991Saha/df914cb31e3afc7714baafd97ee6429a to your computer and use it in GitHub Desktop.
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];
}];
}];
}
//
// 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
//
// 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