SETUP:
Xcode 4.5.2 (4G2008a)
OS X 10.7.5
I have an Xcode project with a normal iOS Application
target. I am trying to add an ApplicationTests
unit test bundle (NOT a LogicTests
bundle) as described by Apple's documentation.
I have carefully followed the steps provided in the documentation, and re-checked everything a few times. However, I cannot build the ApplicationTests target... it fails with a Linker error when trying to link to one of my Appliation's classes. It seems as if setting the Bundle Loader
build setting of the ApplicationTests
target didn't work. But AFAICT, I have set this up correctly, as described by Apple's docs.
MY STEPS TO REPRO:
ApplicationTests
target which imports and exercises a class from the Application
target (e.g. MyAppClass
).Product
> Test
EXPECTED:
Simulator should launch and my ApplicationTests
should run.
ACTUAL:
The ApplicationTests
target fails to build with following error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_MyAppClass", referenced from:
objc-class-ref in ApplicationTests.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I remove references to MyAppClass
in the unit test, the ApplicationTests
bundle will build and run successfully.
Again, it seems like I haven't setup the Bundle Loader
build setting properly in my ApplicationTests
target, but I've re-checked, and I seem to have followed Apple's instructions correctly.
What could I have done wrong to make my ApplicationTests
target not be able to link to symbols in my Application
target?
Update: I have also tried to create new dummy projects from scratch with an Application
and ApplicationTests
targets. In the dummy projects, I add a MyAppClass
class to the Application
target, and am able to link to it and successfully run unit tests which use the MyAppClass
in the dummy ApplicationTests
target.
So Application Tests in my dummy projects work. But they don't work in my real project. I've compared the Targets in the dummy and real projects and cannot find a significant difference which would cause this problem for my real project only.
My real project is quite old (but also very complex, so it would be very difficult to start over). I wonder if the old-ness of the project is preventing this relatively new feature from working?
It still seems to me like I have a build settings problem in one of the targets in my real project. But I can't find it.
Can you try to open the project with Xcode 5.0? Select the unit test target in the project/target settings, open the "General" tab and check if the selected "Target" is your main app target.
That's where Xcode get's the information what to link to the unit test bundle loader.
Related Question: Unit Testing in With A Static Library
In summary, regardless of using SenTest or XCTest, you need to make sure that
Your main project is set as a dependency of your test target
If your main target is a static library, your main target's library product needs to be included in your test target's link with binary libraries
build phases
Missing either (or both) of these can result in this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With