Search code examples
objective-ccocoaunit-testingxcode4

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)

Solution

  • 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