IOS -NSRunLoop in XCTest: How Do I Get A Run Loop to Work in A Unit Test?

前端 未结 2 470
你的背包
你的背包 2020-12-31 19:22

OK. I looked around, and didn\'t find an exact answer to my issue.

I am trying to test a timeout handler in a unit test (not the main run).

The issue seems t

2条回答
  •  庸人自扰
    2020-12-31 19:58

    When you use timers and the main runloop, you will need to manually run the runloop:

    while (continueCondition || !time_way_over_timeout)  {
        [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    

    where continueConditioncould be some flag that indicates that the timeout handler was invoked and time_way_over_timeouta comparision of the current time with a pre-calulated maximum execution time (so you can handle a "timeout of timout test" for your unit test ^^)

    Also see this blog post about asynchronous unit testing: http://dadabeatnik.wordpress.com/2013/09/12/xcode-and-asynchronous-unit-testing/

提交回复
热议问题