当从 UIStoryboard 实例化 ViewController 时,isMemberOfClass 返回 no [英] isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

查看:22
本文介绍了当从 UIStoryboard 实例化 ViewController 时,isMemberOfClass 返回 no的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 OCUnit 测试类:PatientTestViewControllerTests.界面如下:

I have an OCUnit Test class: PatientTestViewControllerTests. Below is the interface:

@interface PatientTestViewControllerTests : SenTestCase

@property (nonatomic, strong) PatientTestViewController *testController;

@end

和设置:

- (void) setUp
{    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Testing" bundle:nil];
    self.testController = [storyboard instantiateInitialViewController];
}

测试"故事板是我的应用中唯一的故事板,并被设置为应用的主故事板.PatientTestViewController 被设置为故事板的唯一视图控制器.

The 'Testing' storyboard is the only storyboard in my app, and is set as the app's main storyboard. The PatientTestViewController is set as the storyboard's only view controller.

我的测试班有一个测试:

I have one test in my test class:

- (void) testInitialTestingStoryboardViewIsPatientTest
{
    STAssertTrue([self.testController isMemberOfClass:[PatientTestViewController class]], @"Instead of the %@, we have %@",[PatientTestViewController class], [self.testController class]);
}

此测试失败并显示以下日志消息:

This test fails with the following log message:

错误:-[PatientTestViewControllerTests testInitialTestingStoryboardViewIsPatientTest]:[self.testController isMemberOfClass:[PatientTestViewController class]]"应该是真的.我们有 PatientTestViewController

error: -[PatientTestViewControllerTests testInitialTestingStoryboardViewIsPatientTest] : "[self.testController isMemberOfClass:[PatientTestViewController class]]" should be true. Instead of the PatientTestViewController, we have PatientTestViewController

这怎么可能?由于

[self.testController isMemberOfClass:[PatientTestViewController 类]]

显然是假的,测试日志怎么会说两者都

is apparently false, how can the test log say that both

[self.testController 类][PatientTestViewController 类]

看起来一样吗?

附加信息:

  • 在测试中使用[self.testController isKindOfClass:[PatientTestViewController class]]也失败
  • 使用 [self.testController class] == [PatientTestViewController class] 也会失败.

使用[self.testController isKindOfClass:[UIViewController class]] PASSES.

推荐答案

问题很可能是您的视图控制器的 .m 文件同时包含在应用程序和测试包中.ocunit(以及 Kiwi 等衍生产品)使用测试工具,使应用中包含的类可用于测试,而无需显式包含它们的实现.

The problem is likely that your view controller's .m file is included in both targets, the app and the test bundle. ocunit (and derivatives like Kiwi) uses a test harness that makes the classes included in the app available to tests without having to explicitly include their implementation.

包含两者会给你同一个类的两个副本,这就是为什么它们具有相同的描述但不同的内存地址.

Including both has given you two copies of the same class, which is why they have the same description but different memory addresses.

这篇关于当从 UIStoryboard 实例化 ViewController 时,isMemberOfClass 返回 no的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆