40

My unit tests worked fine in xcode4 until I had to create a new schema to compile a package to run on my device for testing.

All I did was creating a new Target and a new Scheme, now I try to run the unit tests and get the following error:

The test bundle at [...]Tests.octest could not be loaded because a link error occurred. It is likely that dyld cannot locate a framework framework or library that the the test bundle was linked against, possibly because the framework or library had an incorrect install path at link time.

What did I break?

Haoest
  • 13,610
  • 29
  • 89
  • 105
  • Possibly related and possibly helpful: [one](http://stackoverflow.com/questions/2867251/ocunit-testing-an-embedded-framework) [two](http://stackoverflow.com/questions/6268259/xcode-4-unit-testing-linker-error) – PengOne Jun 20 '11 at 05:04

6 Answers6

47

Like @Haoest and @Peter DeWeese above's comments to Answer 1 - I had exactly the same problem when I changed the product name.

To fix this for the case where you've renamed the product, you need to go to the Build Settings tab of the test target, and change the Linking section - the Debug and Release bundle loader settings. If you have renamed the product - the directory and name of the application may both be incorrect.

Thanks to both of them for pointing this out - but I thought this alternative fix for this situation deserved a higher profile than a comment.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • Also make sure to clean and rebuild the project after making these changes. – Gavin Miller Jul 31 '11 at 20:07
  • Can you confirm my suspicion that you have to hand changing the Bundle Loader because the test target's Target Dependency doesn't correctly update when changing the product name (i.e. what is displayed within the parentheses)? – edelaney05 Jan 21 '12 at 17:29
  • I had to reset my Simulator (iOS Simulator -> Reset Content and Settings...) before this solution would work right. Otherwise Simulator took several minutes to start test, and then tests wouldn't run. – Rich Apodaca May 02 '12 at 23:39
  • 9
    New “Unit Tests” target,Click the Build Settings tab and set the Bundle Loader setting to $(BUILT_PRODUCTS_DIR)/ **RenamedProductName.app** / **RenamedProductName** .While adding UnitTest to existing app ,I have solved error ld: file not found: /Users/HomeDirectory/Library/Developer/Xcode/DerivedData/MyExistingApp-ecvhpixjl‌​crqmyfiiqaqhllwtsgy/Build/Products/Debug-iphonesimulator/MyExistingApp.app/MyExis‌​tingApp. – Alphonse R. Dsouza Jan 25 '13 at 06:55
  • Alphose's explanation solved my problem too. Thanks! – Trausti Kristjansson Oct 04 '13 at 23:02
28

I've also run across problems with Xcode 4 after adding a target to an existing project. I eventually figured out that the Xcode DerivedData for the project was damaged. By deleting that data, I caused Xcode to rebuild the data and the project returned to normal. I found the data in my home Library folder (~/Library/Developer/Xcode/DerivedData/).

Mr. Berna
  • 10,525
  • 1
  • 39
  • 42
  • 13
    That didn't directly fix the test setup, but cleaning the DerivedDatas folder did gave me meaningful clues to finally get the test framework back to work. The reason it broke was because I changed the product name, and the test target were still looking at the old compiled binary. – Haoest Jun 23 '11 at 04:09
  • 4
    I broke mine by changing the product name as well. The annoying thing is that it says "No issues". – Peter DeWeese Jun 27 '11 at 14:23
  • 2
    To delete the derived data, you can also go into the organizer, select the project and press delete next to the derived data. – j-a Jun 13 '12 at 21:26
8

I tried everything (including the other answers and those noted here http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/), but finally found a different solution:

Set Deployment Postprocessing (in the Deployment section of Build Settings) to NO for the Debug target.

Before I did this, the executable was being stripped, and the link would fail with

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_SomeClassUnderTest", referenced from:
      objc-class-ref in SomeTest.o

No matter that Strip Linked Product and Strip Debug Symbols During Copy were set to NO, it made no difference - only changing the Deployment Postprocessing setting finally made sure that the symbols were not stripped.

sdsykes
  • 1,256
  • 12
  • 15
2

Same error message, in my case I wasn't linking one of the classes that were needed during the tests.

alex-i
  • 5,406
  • 2
  • 36
  • 56
2

I discovered my problem was that I had 'Link time optimisation' enabled on my debug build. Setting it to no resolved the problem.

Dan Poltawski
  • 575
  • 4
  • 12
0

We were using nodejs-mobile which would build with the app but fail building with the tests.

Undefined symbols for architecture x86_64:
  _start_node

In addition to all of the previous answers (build settings, search paths, making a brand new Unit Test, deleting DerivedData), what finally resolved it was creating a brand new new UI test instead of a Unit Test in Xcode.

It built successfully. Then you can copy your test definitions from your current test to the one one.

If you do not need the UI part, you can uncomment XCUIApplication().launch() in setUp of the generated tests, which makes it run as fast as before.

tjh
  • 1