Xcode 4 unit testing linker error

蓝咒 提交于 2019-12-03 01:44:17

I had to set the "Test Host" property on the unit test target to $(BUNDLE_LOADER). That solved my problem!

I just wasted hours on this and similar errors - turns out I had renamed my main target - attempts to fix this by renaming the relevant variables and by removing the entire DerivedData directory were unsuccessful.

I eventually simply set up a new unit test target following the steps here: http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/

And everything is fine now.

So - if you have strange, inexplicable link errors you might be better off just creating a new unit test target. Only takes 2 minutes.

I did the following:
http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/

Take a deeper look at Test_Hosts = $(BUNDLE_LOADER)

Setting the Test_Hosts fixed exactly the same issue!

When I get linker errors running unit tests, two things fix the problem for me. The first solution is to set the Test After Build build setting to YES and choose Product > Build For > Build For Testing to run the tests. This solution is the easier one to implement.

The second solution is to add the application's implementation files to the unit test target. Open the file inspector by choosing View > Utilities > File Inspector. Select an implementation file in the project navigator. Select the checkbox next to the unit test target in the file inspector.

For iPhone applications running in the simulator, make sure the Test Host build setting is blank. The simulator does not support application-hosted unit tests.

I had this same problem once. For some reason one of the source files for my project was also included to be compiled for the test target, which causes this link error.

By making sure that only the test-implementation files are compiled, you should be able to resolve this error. You can check this at:

TestTarget -> Build Phases -> Compile Sources

Came across this error running Xcode 4.5.2 - it's Nov 2012 - none of the above worked. It seems that setting the bundle loader and test host is supposed to fill in all the dependencies from your project for you - or run the tests in your app's environment or something, but unfortunately wasn't working for me. What it did do was prevent the specific Xcode warnings about which files/libraries were missing.

What worked for me was adding a new target: cocoa touch unit test, (making sure the bundle loader and test host build settings were empty), watching the build errors and manually adding in the missing dependencies - one by one all the source files from my project that were needed and then the Frameworks. Not very elegant but I was just happy to get it working. Not sure why I haven't tried this GHUnit library yet.

Make sure that the test target has the app target configured as a dependency (Build Phases -> Target Dependencies).

I'm aware that this question is quite old, but I just struggled with identical issue for some time and finally managed to solve it, so let me share what I found.

I've been porting an app from iOS to Mac, the project itself was created for iOS so both the project and main target had iOS in supported platforms. Now, when I started porting I created new target for Mac, and changed supported platforms to OSX only for that target. After that I created another target for unit tests, but forgot to change the supported platforms from iOS to Mac. I think you already should know what the problem was, basically the unit tests target is by default linked with Cocoa framework so since the supported platforms for this target was only iOS the cocoa framework was never built and wasn't properly linked. Changing supported platforms to OSX for the test target fixed the problem.

I'm aware that this may not be very helpful for iOS test targets, but at least go to your test target Link Binary With Libraries section and see if there are any red libraries. This gave me the idea, maybe it will also help some of you.

I've run into this after several merges lately where a coworker and I have both added files to the project, and not all of the implementation files are a member of the unit testing target.

The solution:

  1. Select a small range of your .m files (use "Filter in Navigator" (cmd-opt-j) to search for .m)
  2. Show File Inspector (cmd-opt-1) to view the files' target memberships
  3. Make sure the appropriate files are a member of your test target.

Note: if the test target checkbox shows - instead of + or being unchecked, it means that some, but not all, of the selected files are a member of the target.

I've tried doing more intelligent things at merge time with the .xcodeproj's project.pbxproj file, but I've given up out of frustration with its inscrutability every time and fallen back on this method.

(Another note: I only select a few files at a time for two reasons:

  • Xcode's performance for the file inspector when selecting more than 7–10 files is poor
  • Some files aren't and shouldn't be a member of the test target, and it's easier to do a process of elimination when the selected range is small)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!