+(NSString *) classMethod:(id)someDataObject
{
NSString *returnStr;
//do something with someDataObject and set returnStr
//...
returnStr = [NSString stringWithFormat:@"%@%@", returnStr,[self getCurrentTimestamp]];
return returnStr;
}
+ (NSString *)getCurrentTimestamp
{
NSNumber *t = [NSNumber numberWithLong:[[NSDate date] timeIntervalSince1970]];
return [t stringValue];
}
我正在努力为 classMethod 编写测试用例,因为当我运行 OCUnit 测试并将 OCMock 对象传递给 classMethod 时,输出总是会随着我添加时间戳而不同。存根类方法的最佳方法是什么- getCurrentTimestamp?实际上是否可以存根正在测试的类的类方法?
我试过这样,但它不工作:
id mock = [OCMockObject mockForClass:[MyClass class]];
[[[mock stub] andReturn:@1378468455] getCurrentTimestamp];
NSString *str = [MyClass classMethod:someMockObject];