0

I have a method using promises (PMKPromise), that I'd like to unit test using the library Kiwi.

My method looks like this:

- (void)fetchData {
[PMKPromise new:^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject) {
    // make the request
}].then(^(NSDictionary *data){
    // code that I'd like to test
});
}

I want to capture and execute the promise completion block (called "then"), and I managed to do it this way (with a mocked promise):

// capture the promise completion block
__block void (^capturedBlock)(NSDictionary *);

[thePromise stub:@selector(then) withBlock:^id(NSArray *params) {
    return ^(id block){
        capturedBlock = block;
        return nil;
    };
}];

[myObj fetchData];

// execute the block
capturedBlock(@{});

What I'd like to do now is to hide this ugly code in a category that I could easily use in the future when unit testing promises.

I'd like to pass a block "double pointer" as a method argument.

This could be the implementation:

@interface NSObject (PMKPromiseTest)

- (void)captureThenBlock:(void (^ *)(NSDictionary *))capturedBlock;

@end

@implementation NSObject (PMKPromiseTest)

- (void)captureThenBlock:(void (^ *)(NSDictionary *))capturedBlock {

    [self stub:@selector(then) withBlock:^id(NSArray *params) {
        return ^(id block){
            *capturedBlock = block;
            return nil;
        };
    }];
}

@end

And this is how I would use it:

// capture the promise completion block:
void (^capturedBlock)(NSDictionary *);
[thePromise captureThenBlock:&capturedBlock];

[myObj fetchData];

// execute it
capturedBlock(@{});  // Exception! captureBlock is nil

Unfortunately this code does not work. When stepping in, the block is indeed captured. But once out, at the moment of executing the captured block, it is nil.

0 Answers0