Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4 unit testing linker error

NOTE: "Use GHUnit" is not an acceptable answer to this question. I know most think GHUnit is better than the Xcode4 OCUnit, but that's not what I'm asking about. I'll evaluate that separately.

I have an Xcode project that I created in Xcode4 from scratch, with the "Include unit tests" checkbox checked during creation. I have also included some libraries I developed in a previous project. They were added to the project via the "Add Files to x..." dialog and only added to the application target (not the testing target). They work fine when running the application, so I think they're set up correctly. I also have a number of various classes written for this project.

My testing files are set up in the standard way, named [AppName]Tests.h and .m.
Code for header:
#import < SenTestingKit/SenTestingKit.h >

@interface [AppName]Tests : SenTestCase {
@private
}
@end

Code for implementation:
#import "[AppName]Tests.h"

@implementation [AppName]Tests

- (void)setUp
{
    [super setUp];
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    [super tearDown];
}  
// Test methods go here
@end

Which is just the basic skeleton. It works fine in my other project, and in this project as long as I don't import any other files. When I do import another file from this project and use it, I see the following error in the Xcode output log:
The test bundle at /Users/[Me]/Library/Developer/Xcode/DerivedData/[AppName]-dwuuuwcpmdqxqmgxomoniplwhlpb/Build/Products/Debug-iphonesimulator/[AppName]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.

I've already verified that:

  1. All the frameworks I use have been added to "Link Binary with Libraries" for both the app and test targets.
  2. The test target has been configured to build correctly and that all my test methods show up in Edit Scheme...->Test->Tests
  3. Every issue but this one has been resolved and there are no compiler errors.
  4. All the settings discussed here are set up correctly and identical to my other project that tests correctly.

Any thoughts on what might be causing this?

like image 953
Kongress Avatar asked Jun 07 '11 16:06

Kongress


2 Answers

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

like image 177
Sam Grossberg Avatar answered Sep 21 '22 20:09

Sam Grossberg


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.

like image 35
n13 Avatar answered Sep 22 '22 20:09

n13