GHAsyncTestCase Class Reference
| Inherits from | GHTestCase : NSObject |
| Declared in | GHAsyncTestCase.h GHAsyncTestCase.m |
Overview
Asynchronous test case with wait and notify.
If notify occurs before wait has started (if it was a synchronous call), this test case will still work.
Be sure to call prepare before the asynchronous method (otherwise an exception will raise).
@interface MyAsyncTest : GHAsyncTestCase { }
@end
@implementation MyAsyncTest
- (void)testSuccess {
// Prepare for asynchronous call
[self prepare];
// Do asynchronous task here
[self performSelector:@selector(_succeed) withObject:nil afterDelay:0.1];
// Wait for notify
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}
- (void)_succeed {
// Notify the wait. Notice the forSelector points to the test above.
// This is so that stray notifies don't error or falsely succeed other tests.
// To ignore the check, forSelector can be NULL.
[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testSuccess)];
}
@end
Tasks
-
runLoopModesRun loop modes to run while waiting; Defaults to NSDefaultRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode
property -
– preparePrepare before calling the asynchronous method.
-
– prepare:Prepare and specify the selector we will use in notify.
-
– waitForStatus:timeout:Wait for notification of status or timeout.
-
– waitFor:timeout:kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
-
– waitForTimeout:Wait for timeout to occur. Fails if we did NOT timeout.
-
– notify:forSelector:Notify waiting of status for test selector.
-
– notify:Notify waiting of status for any selector.
-
– runForInterval:Run the run loops for the specified interval.
Instance Methods
notify:
Notify waiting of status for any selector.
- (void)notify:(NSInteger)statusParameters
- status
Status, for example, kGHUnitWaitStatusSuccess
Declared In
GHAsyncTestCase.hnotify:forSelector:
Notify waiting of status for test selector.
- (void)notify:(NSInteger)status forSelector:(SEL)selectorParameters
- status
Status, for example, kGHUnitWaitStatusSuccess
- selector
If not NULL, then will verify this selector is where we are waiting. This prevents stray asynchronous callbacks to fail a later test.
Declared In
GHAsyncTestCase.hprepare
Prepare before calling the asynchronous method.
- (void)prepareDeclared In
GHAsyncTestCase.hprepare:
Prepare and specify the selector we will use in notify.
- (void)prepare:(SEL)selectorParameters
- selector
Selector
Declared In
GHAsyncTestCase.hrunForInterval:
Run the run loops for the specified interval.
- (void)runForInterval:(NSTimeInterval)intervalParameters
- interval
Interval @author Adapted from Robert Palmer, pauseForTimeout
Declared In
GHAsyncTestCase.hwaitFor:timeout:
kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
- (void)waitFor:(NSInteger)status timeout:(NSTimeInterval)timeoutParameters
- status
kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
- timeout
Timeout in seconds @deprecated Use waitForTimeout:
Declared In
GHAsyncTestCase.hwaitForStatus:timeout:
Wait for notification of status or timeout.
- (void)waitForStatus:(NSInteger)status timeout:(NSTimeInterval)timeoutParameters
- status
kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
- timeout
Timeout in seconds
Discussion
Be sure to prepare before calling your asynchronous method. For example,
- (void)testFoo {
[self prepare];
// Do asynchronous task here
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}
Declared In
GHAsyncTestCase.h