Testing asynchronous call in unit test in iOS

后端 未结 12 1508
-上瘾入骨i
-上瘾入骨i 2020-12-24 08:51

I am facing a problem while unit testing an asynchronous call in iOS. (Although it is working fine in view controllers.)

Has anyone faced this issue before? I have t

12条回答
  •  既然无缘
    2020-12-24 09:46

    Here's another alternative, XCAsyncTestCase, that works well with OCMock if you need to use it. It's based on GHUnit's async tester, but is uses the regular XCTest framework instead. Fully compatible with Xcode Bots.

    https://github.com/iheartradio/xctest-additions

    Usage is the same, just import and subclass XCAsyncTestCase.

    @implementation TestAsync
    - (void)testBlockSample
    {
        [self prepare];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){
            sleep(1.0);
            [self notify:kXCTUnitWaitStatusSuccess];
        });
        // Will wait for 2 seconds before expecting the test to have status success
        // Potential statuses are:
        //    kXCTUnitWaitStatusUnknown,    initial status
        //    kXCTUnitWaitStatusSuccess,    indicates a successful callback
        //    kXCTUnitWaitStatusFailure,    indicates a failed callback, e.g login operation failed
        //    kXCTUnitWaitStatusCancelled,  indicates the operation was cancelled
        [self waitForStatus:kXCTUnitWaitStatusSuccess timeout:2.0];
    }
    

提交回复
热议问题