Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UnitTests: Can't link to symbols in my Application target. Whats wrong?

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:

  1. Follow Apple's documentation for setting up an ApplicationTests bundle.
  2. Write a unit test method in the ApplicationTests target which imports and exercises a class from the Application target (e.g. MyAppClass).
  3. Select the Application Target in the Scheme popup, and iPhone 6.0 simulator in the run destination popup
  4. 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.

like image 220
Todd Ditchendorf Avatar asked Oct 05 '22 19:10

Todd Ditchendorf


2 Answers

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.

like image 180
Nikolai Ruhe Avatar answered Oct 10 '22 01:10

Nikolai Ruhe


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.

like image 39
JRG-Developer Avatar answered Oct 10 '22 03:10

JRG-Developer