Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stub method with block of code as parameter with OCMock

Is there a way to stub method, that takes block as it's parameter? For example mehod:

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
like image 441
kraag22 Avatar asked May 28 '12 08:05

kraag22


1 Answers

Yes. The easiest way would be to accept anything:

id mockGeocoder = [OCMockObject mockForClass:[CLGeocoder class]];
[[mockGeocoder stub] reverseGeocodeLocation:[OCMOCK_ANY] completionHandler:[OCMOCK_ANY]];

It gets a bit trickier if you want to verify a particular block is passed in. One option is to make your completion handler a property of your class, initialize it when you initialize your class, and have the test match it directly:

// in your class
@property(copy)CLGeocodeCompletionHandler completionHandler;

// in your class's init method
self.completionHandler = ^(NSArray *placemark, NSError *error) {
    //
}

// using the completion handler
[geocoder reverseGeocodeLocation:location completionHandler:self.completionHandler];

// the test
id mockGeocoder = [OCMockObject mockForClass:[CLGeocoder class]];
[[mockGeocoder stub] reverseGeocodeLocation:[OCMOCK_ANY] completionHandler:yourClass.completionHandler];
like image 172
Christopher Pickslay Avatar answered Nov 18 '22 07:11

Christopher Pickslay