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

Properties

runLoopModes

Run loop modes to run while waiting; Defaults to NSDefaultRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode

@property (strong, nonatomic) NSArray *runLoopModes

Declared In

GHAsyncTestCase.h

Instance Methods

notify:

Notify waiting of status for any selector.

- (void)notify:(NSInteger)status

Parameters

status

Status, for example, kGHUnitWaitStatusSuccess

Declared In

GHAsyncTestCase.h

notify:forSelector:

Notify waiting of status for test selector.

- (void)notify:(NSInteger)status forSelector:(SEL)selector

Parameters

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.h

prepare

Prepare before calling the asynchronous method.

- (void)prepare

Declared In

GHAsyncTestCase.h

prepare:

Prepare and specify the selector we will use in notify.

- (void)prepare:(SEL)selector

Parameters

selector

Selector

Declared In

GHAsyncTestCase.h

runForInterval:

Run the run loops for the specified interval.

- (void)runForInterval:(NSTimeInterval)interval

Parameters

interval

Interval @author Adapted from Robert Palmer, pauseForTimeout

Declared In

GHAsyncTestCase.h

waitFor:timeout:

kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status

- (void)waitFor:(NSInteger)status timeout:(NSTimeInterval)timeout

Parameters

status

kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status

timeout

Timeout in seconds @deprecated Use waitForTimeout:

Declared In

GHAsyncTestCase.h

waitForStatus:timeout:

Wait for notification of status or timeout.

- (void)waitForStatus:(NSInteger)status timeout:(NSTimeInterval)timeout

Parameters

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

waitForTimeout:

Wait for timeout to occur. Fails if we did NOT timeout.

- (void)waitForTimeout:(NSTimeInterval)timeout

Parameters

timeout

Timeout

Declared In

GHAsyncTestCase.h