我一直在为 iOS 编程进行单元测试,在尝试通过使用 OCMock 模拟它的子类然后查看主类是否添加子控制器来验证我的主类时遇到了一个小问题(mockObjects [ uiviewContoller]),然后验证对象是否调用了每个子控制器上的方法。
问题是我不断收到“测试失败'添加根视图控制器 OCMockObject[UiViewController] 作为视图控制器的子级'”
每隔一次我运行测试。
- (void)setUp
{
[super setUp];
testMain = [[UIViewController alloc] init];
}
- (void)tearDown
{
for (UIViewController *testCon in testMain.childViewControllers) {
[testCon removeFromParentViewController];
}
testMain = nil;
[super tearDown];
}
测试:
- (void) testDayNightTriggerTriggersAllSubviews{
id mockTopController = [OCMockObject niceMockForClass:[UIViewController class]];
id mockBottomController = [OCMockObject niceMockForClass:[UIViewController class]];
id mockMainScreen = [OCMockObject niceMockForClass:[UIViewController class]];
[[mockTopController expect] dayNightTrigger];
[[mockBottomController expect] dayNightTrigger];
[[mockMainScreen expect] dayNightTrigger];
//trigger
[testMain dayNightTrigger:mockTopController bottom:mockBottomController main:mockMainScreen];
[mockBottomController verify];
[mockTopController verify];
[mockMainScreen verify];
}
验证方法:
//overload
- (void) dayNightTrigger:(UIViewController *) top bottom:(UIViewController *)bottom main:(UIViewController *)main{
self.bottomMenu = bottom;
self.topMenu = top;
self.mainScreen = main;
[self dayNightTrigger];
}
- (void) dayNightTrigger{
[self.app dayNightTrigger];
[self.bottomMenu dayNightTrigger];
[self.topMenu dayNightTrigger];
[self.mainScreen dayNightTrigger];
}
我想知道我的设置/拆卸是否有任何问题?或者我在 OCMock 框架上做错了什么,但真的是为什么我一直收到这个错误。