【发布时间】:2012-09-12 10:30:25
【问题描述】:
我正在开发一个 iOS5+ 项目(xcode 4.4.1 SDK 5.1)
我在单元测试中有这段代码:
[_appDelegate application:nil didFinishLaunchingWithOptions:nil];
UITabBarController *tabBarController = (UITabBarController*)_appDelegate.window.rootViewController;
NSArray *viewControllers = [tabBarController viewControllers];
UINavigationController *nc_1 = [viewControllers objectAtIndex:0];
UIViewController *vc_1 = nc_1.topViewController;
STAssertTrue([vc_1 isKindOfClass:[ScheduleViewController class]]==YES, @"UITabBarController first tab should be a ScheduleViewController class");
如果我运行测试,测试失败。
所以我用调试器检查:
(lldb) po [ScheduleViewController class]
(id) $1 = 0x00142b04 ScheduleViewController
(lldb) po vc_1
(UIViewController *) $2 = 0x11a32dc0 <ScheduleViewController: 0x11a32dc0>
(lldb) print (BOOL) [vc_1 isKindOfClass:(Class)[ScheduleViewController class]]
(BOOL) $4 = YES
(lldb) po [vc_1 class]
(id) $5 = 0x00142b04 ScheduleViewController
(lldb)
在应用程序中:didFinishLaunchingWithOptions:我创建了一个 ScheduleViewController 并将其用作导航控制器的 rootController。调试器说它是正确的。 我不明白上面的断言有什么问题。
有人知道这个吗?
更新
断言的第一个实现是:
STAssertTrue([vc_1 isKindOfClass:[ScheduleViewController class]], @"UITabBarController first tab should be a ScheduleViewController class");
断言同样失败。
更新 2
正如评论中所建议的,我尝试在断言之前添加这段代码:
BOOL vcBool = [vc_1 isKindOfClass:[ScheduleViewController class]];
使用我看到的调试器:
(lldb) print (BOOL) [vc_1 isKindOfClass:(Class)[ScheduleViewController class]]
(BOOL) $1 = YES
(lldb) print (BOOL) vcBool
(BOOL) $2 = NO
(lldb)
更新 3
按照 cmets 中的建议,我在断言之前添加了这一行:
NSLog(@"vc_1=%@ class=%@", vc_1, NSStringFromClass([vc_1 class]));
从调试控制台:
vc_1=<ScheduleViewController: 0x993bdb0> class=ScheduleViewController
【问题讨论】:
-
您是否尝试删除
==YES? -
@dasdom
== YES是不必要的,但它对功能有影响吗? -
你确定它在
STAssertTrue()声明中失败了,而不是在其他地方? -
如果你在失败的测试上面添加这一行,我敢打赌它也会失败。您的代码很可能遗漏了 OCUnit 的某些初始化步骤:'STAssertTrue(YES, @"Even YES failed");'如果奇迹发生,则从 STAssert 中提取您的语句并将值分配给布尔值,记录它,然后在 BOOL 上进行 STAssert。
-
所以在 'BOOL vcBool' 之前做更多的日志记录: NSLog(@"vc_1=%@ class=%@", vc_1, NSStringFromClass([vc_1 class]));'我猜 vc_1 是 nil,但是当你到达调试器时,它已经设置好了。
标签: objective-c ios xcode llvm nsobject