I'm writing unit tests to my Facebook SDK wrapper and something got me confused about NSInvocation's - (void)getArgument:(void *)buffer atIndex:(NSInteger)index
while trying to mock FBRequest with OCMock.
Here's the method definition I'm trying to test.
-(void) friendListInstalledOnly:(BOOL) installedOnly withCompletionHandler:(FacebookHelperCompletionHandler)handler
where FacebookHelperCompletionHandler is a typedef as follows:
typedef void (^FacebookHelperCompletionHandler)(id result, NSError *error);
Here is my test method
-(void) testFriendsListInstalledOnly {
id mockRequest = [OCMockObject mockForClass:[FBRequest class]];
NSArray *friendsArray = [NSArray arrayWithObjects:
@{@"id":@(1),@"name":@"Kaan"},
@{@"id":@(2),@"name":@"Mumtaz",@"installed":@(1)},
@{@"id":@(3),@"name":@"Toprak",@"installed":@(0)},
@{@"id":@(4),@"name":@"Yeter"},nil];
NSDictionary<FBGraphObject> *responseDictionary = [FBGraphObject graphObjectWrappingDictionary:@{@"data":friendsArray}];
[[mockRequest expect] setGraphPath:[OCMArg any]];
[[[mockRequest expect] andDo:^(NSInvocation *invocation) {
FBRequestHandler requestHandler = nil;
[invocation getArgument:&requestHandler atIndex:3];
requestHandler(nil,responseDictionary,nil);
}] startWithCompletionHandler:[OCMArg any]];
self.facebookHelper.friendsRequest = mockRequest;
[self.facebookHelper friendListInstalledOnly:YES withCompletionHandler:^(id result, NSError *error) {
NSLog(@"%@",result); // The actual assert will go here.
}];
}
When I pass 3 as index in the NSInvocation method, I get an error saying that index 3 is out of bounds. But the documentation says that index 0 is self, 1 is _cmd, so I believe that installedOnly should be 2 and completionHandler should be 3. I played with it and passing 2 for index gets me my desired completion handler and test works. I just want to understand why these indexes are working wrong (maybe something with primitive types?)
EDIT: Of course, stupid me... I was mocking FBRequest's - (FBRequestConnection*)startWithCompletionHandler:(FBRequestHandler)handler
method where the handler is at index 2. I'm leaving the question just in case someone needs block stubbing with OCMock in the future.
`