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

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

问题描述

我有一个OCUnit Test类:PatientTestViewControllerTests。下面是界面:

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

@interface PatientTestViewControllerTests : SenTestCase

@property (nonatomic, strong) PatientTestViewController *testController;

@end

和setUp:

- (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]]应为true。而不是 PatientTestViewController ,我们有 PatientTestViewController

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

这怎么可能?由于

[self.testController isMemberOfClass:[PatientTestViewController class]]

显然是假的,测试日志怎么说两个

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

[self.testController class] [PatientTestViewController类]

看起来一样吗?

其他信息:


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

  • 使用 [self.testController class] == [ PatientTestViewController类] 也失败。

  • using [self.testController isKindOfClass:[PatientTestViewController class]] in the test also fails
  • using [self.testController class] == [PatientTestViewController class] fails also.

使用 [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天全站免登陆