+(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];
}
I am struggling to write test case for the classMethod because when I run OCUnit test and pass OCMock object to classMethod the output will be always different as I add timestamp. What is the best approach to stub class method - getCurrentTimestamp? Is it actually possible to stub class methods of the class that is being tested?
I tried like this, but it is not working:
id mock = [OCMockObject mockForClass:[MyClass class]];
[[[mock stub] andReturn:@1378468455] getCurrentTimestamp];
NSString *str = [MyClass classMethod:someMockObject];
Thank you @BenFlynn. Your example (as well as mine) is working indeed. The problem was that "MyClass.h" was checked in Target Membership for the unite test too. "MyClass.h" should target only project (I am not sure why, maybe someone can explain it).