【发布时间】:2025-11-24 21:35:01
【问题描述】:
我是 OCUnit 和 OCMock 的新手,想了解有关此测试方法的更多信息。
我知道 OCUnit 和 OCMock 创建存根生成模拟对象等的能力...
我有一个特定的用例,但我还不能破解。
-(bool) isGameCenterAvailable
{
// Check for presence of GKLocalPlayer API.
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
// The device must be running running iOS 4.1 or later.
bool isIPAD = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
NSString *reqSysVer = (isIPAD) ? @"4.2" : @"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
这是我对单元测试的问题:
1) NSClassFromString(@"GKLocalPlayer") 是对foundation.h 的调用,据我所知,它无法存根。
2) [[UIDevice currentDevice] systemVersion] 是函数作用域的本地调用。我的方法调用另一个类(UIDevice)中的方法我想用存根覆盖他们的函数调用,以返回一个固定的答案来练习这个函数的每个路径。
如果类在被测函数的范围内实例化,则不确定是否可以模拟类。
此外,如何测试 #1 之类的类方法。
重构是这里唯一的答案吗?
【问题讨论】:
标签: objective-c ocunit ocmock