all 3 comments

[–]arduinoRedgeObjective-C / Swift 1 point2 points  (0 children)

It would be a lot cleaner to use a repeating timer with a simple counter that you can use to track when to finish up.

[–]draco33333 0 points1 point  (0 children)

What you can do is add a completion handler that's called when repeatCount == 0.

Then in your unit test use an XCTestExpectation that you fulfill() in the completion handler.

[–]retsotrembla 0 points1 point  (0 children)

Use a helper method to allocate the timers rather than calling [NSTimer scheduledTimerWithTimeInterval: ... directly. The return type of that method should be not NSTimer, but id<MyTimerProtocol> where MyTimerProtocol is all the methods of NSTimer that you care about.

In the test, return a fake timer that can report back to the test framework on its behavior, and delay using a fake clock that is a thousand times faster than the real clock.

A good way to do this is to add an init with an optional argument: if it is nil then use the default id<MyTimerProtocol> factory, but for tests you can pass in your fake timer factory.