NSInvocation getArgument: atIndex: confusion when testing blocks with OCMock

I am writing unit tests in my Facebook SDK shell, and something confused me about NSInvocation - (void)getArgument:(void *)buffer atIndex:(NSInteger)index , trying to mock FBRequest with OCMock.

Here is the definition of the method 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 testing 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 an index in the NSInvocation method, I get an error message indicating 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 completeHandler should be 3. I played with it, and passing 2 for the index gave me my desired completion handler and test. I just want to understand why these indexes are not working properly (maybe something with primitive types?)

EDIT : Of course, I’m stupid ... I mocked the FBRequest method - (FBRequestConnection*)startWithCompletionHandler:(FBRequestHandler)handler , where the handler is at index 2. I leave the question just in case someone needs an OCMock lock in the future . `

+4
source share

Source: https://habr.com/ru/post/1493914/


All Articles