A Trick!

After spending a little while trying to figure out how to wrap tests around a bit of code that looked pretty similar to this:

Block within a block
1
2
3
4
5
6
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.service getAThingWithSuccess:^() {
                  [self doOtherStuff];
              } andFailure:^{
                    [self ohNoes];
               }];

so I could get at the [self doOtherStuff] parts and test them, I found this stub:withBlock: selector in Kiwi. So, I tried the following:

Auto-execute the blocks!
1
2
3
4
5
6
[[NSOperationQueue mainQueue] stub:@selector(addOperationWithBlock:)
                       withBlock:^id(NSArray *params) {
                              void(^block)(void) = params[0];
                              block();
                              return nil;
                         }];

and it worked perfectly! Woot! In my particular case, I was actually using a kiwi spy to capture that success block sent to the service (though maybe I’ll use this new trick instead), so after calling block() in that stub, I was able to immediately get the argument from the spy, whereas all other attempts at trickery were giving me “argument not captured” issues.

Comments

Comments