1

According to this Stackoverflow post: Selectors or Blocks for callbacks in an Objective-C library ,

blocks seem to be the future of ObjC. However, much like anonymous functions, blocks feel more like "drafting" an implementation. Also, due to its "embedded" nature, I fear that overusing them will break modularity in the sense of unit-testing or "testable" OOP.

I couldn't find much guideline on how to test blocks and how to coordinate tests for blocks and regular methods. Are there good resources for this topic?

3 Answers 3

17

I created 3 macros that wait for the block to be executed in a unit test so the assertions can be made inside the block.

#define TestNeedsToWaitForBlock() __block BOOL blockFinished = NO
#define BlockFinished() blockFinished = YES
#define WaitForBlock() while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)

Example:

- (void)testWaitForBlock {
    TestNeedsToWaitForBlock();

    [target selectorWithInlineBlock:^(id obj) {

        // assertions

        BlockFinished();
    }];

    WaitForBlock();
}
3
  • I like the macros and using them in my new tests. Thanks!
    – mikebz
    Commented Oct 5, 2014 at 15:17
  • I don't suppose you have an update for this using Xcode 10.2.1, only ask as I keep getting "Implicit declaration of function 'TestNeedsToWaitForBlock' is invalid in C99" warnings?
    – Jonas
    Commented Jun 7, 2019 at 8:45
  • Yeah, sorry @Jonas, I haven't programmed in Obj-C for years now. Commented Jun 12, 2019 at 5:19
4

Not sure if you've already tried it, but I use Kiwi for unit testing my iOS applications. Its not amazingly documented but it can be used for testing blocks.

https://github.com/allending/Kiwi

Take a look at the 'capturing arguments' under 'mocks and stubs' on their wiki. You can use this to capture a block thats being passed. This is really useful for code thats asynchronous - you can call the method you want to test, capture some completion block and then immediately execute the block synchronously, making your asynchronous code effectively synchronous.

In reference to blocks feeling like drafting an implementation - they don't have to be like that. I define blocks as a would a method, not inline. In fact I often write a method to return the block, making the code clean and easily testable.

Not sure if thats what you were looking for.

2
  • Thanks Chris. Very helpful tips and I think they are what I need.
    – kakyo
    Commented Jun 9, 2013 at 19:12
  • Thank you! It is my first answer on stackoverflow.
    – Chris
    Commented Jun 9, 2013 at 19:32
0
- (void)testWaitForBlock {
    [target selectorWithInlineBlock:^(id obj) {

        // assertions

        BlockFinished();
    }];
   //use this to keep runloop is alive ,you can do anything.
   NSDate * date = [NSDate dateWithTimeIntervalSinceNow:10];
    [[NSRunLoop currentRunLoop]runUntilDate:date];
}
2
  • Please check this URLit will be useful to raise your content quality up Commented Jun 29, 2016 at 2:44
  • Comments and description would be appreciated to understand your suggestion.
    – XLE_22
    Commented Apr 14, 2017 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.