我正在尝试模拟一个类方法,它似乎没有按预期工作。有什么想法吗?
@implementation ApplicationVersionManager
+ (NSString *)appVersion {
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
@end
测试
- (void)testSomething {
OCMStub([ApplicationVersionManager appVersion]).andReturn(@"3.0");
NSString *version = [ApplicationVersionManager appVersion];
// version is nil. Why?
}
答案 0 :(得分:1)
虽然OCMock可以在现有对象上存根方法,但仍需要一个模拟对象来设置存根。以下应该有效:
- (void)testSomething {
id mock = OCMClassMock([ApplicationVersionManager class]);
OCMStub([mock appVersion]).andReturn(@"3.0");
NSString *version = [ApplicationVersionManager appVersion];
}