stub method with block of code as parameter with OCMock

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 06:04:43

问题


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

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

回答1:


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];


来源:https://stackoverflow.com/questions/10781787/stub-method-with-block-of-code-as-parameter-with-ocmock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!