Created
July 8, 2015 18:33
-
-
Save briandw/59bf5a06afe8af67a690 to your computer and use it in GitHub Desktop.
Use this in your Xcode test cases to wait for something to happen while waiting on an exception
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 <XCTest/XCTest.h> | |
@interface XCTestCase (RLWaitWithExpectation) | |
- (void)waitFor:(NSTimeInterval)seconds withExpectationBlock:(BOOL (^)())block; | |
@end | |
@implementation XCTestCase (RLWaitWithExpectation) | |
- (void)waitFor:(NSTimeInterval)seconds withExpectationBlock:(BOOL (^)())block | |
{ | |
NSDate *timeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:seconds]; | |
while (YES) | |
{ | |
if ([timeoutDate timeIntervalSinceNow] < 0 || !block()) | |
{ | |
return; | |
} | |
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; | |
} | |
} | |
@end | |
//example | |
/* | |
XCUIApplication *app = [[XCUIApplication alloc] init]; | |
XCUIElement *button = app.navigationBars[@"NavBar"].buttons[@"Next"]; | |
XCTestExpectation *expectation = [self expectationWithDescription:@"waiting"]; | |
[self waitFor:8.0 withExpectationBlock:^BOOL() | |
{ | |
if (button.exists && button.enabled) | |
{ | |
[expectation fulfill]; | |
return YES; | |
} | |
return NO; | |
}]; | |
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError *error) | |
{ | |
if (error) | |
{ | |
XCTFail(@"Expectation Failed with error: %@", error); | |
} | |
}]; | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment