Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

objective c - isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

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

@interface PatientTestViewControllerTests : SenTestCase

@property (nonatomic, strong) PatientTestViewController *testController;

@end

and setUp:

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

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:

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

How can this be? Since

[self.testController isMemberOfClass:[PatientTestViewController class]]

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

[self.testController class] and [PatientTestViewController class]

look the same?

Additional Info:

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

  • using [self.testController isKindOfClass:[UIViewController class]] PASSES.

  • using [self.testController isMemberOfClass:[UIViewController class]] FAILS.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...