我正在尝试使用 ocmock 学习单元测试。我发现很难从我正在单元测试的类中模拟另一个类的调用。
有人可以建议如何对 KeyChainUtils 类和 HttpRequest 类进行模拟调用:
使用 OCMock 进行单元测试的代码:
@implementation UserProfileService {
+(BOOL) isValidUser
{
NSString* userId = [KeyChainUtil loadValueForKey:USER_ID]; //mock this call
bool isValidUser = NO;
if(userId && userId.length > 0){
NSDictionary* response = [HTTPDataService getJSONForURL:@"http://xtest.com/checkuserid" forRequestData:@{@"userid": userId}];
if(response && response[@"valid"]){
isValidUser = [response[@"valid"] boolValue];
}else{
NSLog(@"error in connecting to server. response => %@", response);
}
}
return isValidUser;
}
}