有没有办法使用存根方法,它接受块作为参数?例如mehod:
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
答案 0 :(得分:6)
是。最简单的方法是接受任何事情:
id mockGeocoder = [OCMockObject mockForClass:[CLGeocoder class]];
[[mockGeocoder stub] reverseGeocodeLocation:[OCMOCK_ANY] completionHandler:[OCMOCK_ANY]];
如果要验证传入的特定块,则会变得有点棘手。一个选项是使您的完成处理程序成为您的类的属性,在初始化类时初始化它,并让测试直接匹配它:
// 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];