Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isKindOfClass: returns false negative in unit test bundle

So I have an instance of MyViewController in the detail view of a UISplitViewController. I am running a unit test to see whether the detail view contains the correct type of view.

I test the type of controller in the unit test with the following:

[controller isKindOfClass:[MyViewController class]];

However, the isKindOfClass method always returns NO

When I po the object in the debugger I get the following:

(gdb) po controller
<MyViewController: 0xb31c4d0>

I have also tried the isMemberOfClass: method, it yields the same results. Can anyone explain why this would happen?

EDIT: So after reading the article posted by Nick Weaver I realised that I was including my app's source files in the test bundle's compile sources build phase. This was also indicated in the log by statements similar to following:

Class MyViewController is implemented in both /Users/jdoe/Library/Application Support/iPhone Simulator/4.3.2/Applications/670A077A-BAD8-4FA6-945A-851F33114CF5/MyApp.app/MyApp and /Users/jdoe/Library/Developer/Xcode/DerivedData/MyApp-drxyfejeattjwgantzesgensnlnx/Build/Products/Debug-iphonesimulator/MyAppTests.octest/MyAppTests. One of the two will be used. Which one is undefined.

However, when I remove the source files from the test bundle's compile sources build phase, I would get a linker error that looks like the following:

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_MyViewController", referenced from:
  objc-class-ref in _MyViewControllerTests.o
 (maybe you meant: _OBJC_CLASS_$__MyViewControllerTests)
like image 461
nduplessis Avatar asked Apr 22 '11 12:04

nduplessis


2 Answers

As mentioned in my question, I realised I was incorrectly including the app's source files in the test bundle's compile sources build phase. After removing the source files from this build phase I solved the linker error of the missing symbols by changing the Symbols Hidden by Default build setting to "No" for the Debug configuration

Xcode build settings

This solved the linker error and meant that I was no longer including duplicate source files


Note: Also, be sure to set the "Host Target" for the test target in the Info tab of Xcode so it'll pull the compile sources from there

like image 115
nduplessis Avatar answered Nov 12 '22 16:11

nduplessis


Maybe this is helpful, first answer: isKindOfClass and NSStringFromClass disagree about UIApplicationDelegate.

like image 28
Nick Weaver Avatar answered Nov 12 '22 15:11

Nick Weaver