带有代码块的存根方法作为OCMock的参数 [英] stub method with block of code as parameter with OCMock
本文介绍了带有代码块的存根方法作为OCMock的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用存根方法,它将块作为参数?例如mehod:
Is there a way to stub method, that takes block as it's parameter? For example mehod:
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
推荐答案
是的。最简单的方法是接受任何东西:
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];
这篇关于带有代码块的存根方法作为OCMock的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文