我想测试一种使用逆向地理编码的方法。我想做的是:
问题是MKReverseGeocoder坐标属性是只读的,我只能在构造函数方法中设置它:
[[MKReverseGeocoder alloc] initWithCoordinate:coord]当然,坐标只适用于我想要测试的方法。
有人知道我怎么能嘲笑MKReverseGeocoder类吗?
提前谢谢文森特。
发布于 2010-05-07 18:26:04
看看Matt Gallagher关于Cocoa应用程序单元测试的优秀文章。他为NSObject提供了一个类别扩展,允许您在测试时替换实例。我也用它做过类似的事。我想你的测试会是这样的:
#import "NSObject+SupersequentImplementation.h"
id mockGeocoder = nil;
@implementation MKReverseGeocoder (UnitTests)
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
if (mockGeocoder) {
// make sure the mock returns the coordinate passed in
[[[mockGeocoder stub] andReturn:coordinate] coordinate];
return mockGeocoder;
}
return invokeSupersequent(coordinate);
}
@end
...
-(void) testSomething {
mockGeocoder = [OCMockObject mockForClass:[MKReverseGeocoder class]];
[[mockGeocoder expect] start];
// code under test
[myObject geocodeSomething];
[mockGeocoder verify];
// clean up
mockGeocoder = nil;
}https://stackoverflow.com/questions/2766701
复制相似问题