Skip to content

Instantly share code, notes, and snippets.

@briandw
Created July 8, 2015 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandw/59bf5a06afe8af67a690 to your computer and use it in GitHub Desktop.
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
#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