Class Foo is implemented in both MyApp and MyAppTestCase. One of the two will be used. Which one is undefined

Objective CXcodeCocoaMacos

Objective C Problem Overview


Recently I started Unit testing my application. This project (in Xcode4) was created without a unit test bundle so I had to set it up. I have followed the steps from here: http://cocoawithlove.com/2009/12/sample-mac-application-with-complete.html And It was working well for simple classes but now I am trying to test a class that depends on another and that on another, etc.

First I got a linker error so I added *.m files to the test case target but now I get a warning for every class I am trying to test:

> Class Foo is implemented in both MyApp > and MyAppTestCase. One of the two will > be used. Which one is undefined.

I wonder why is that? How can I solve this? Maybe I missed something when setting the unit test target?

Edit - The Solution
  • Set "Bundle Loader" correctly to $(BUILT_PRODUCTS_DIR)/AppName.app/AppName

  • Set "Symbols Hidden by Default" to NO (in Build Settings of the target application). This is where the linker errors come from because it is YES by default!. I've been struggling with this for so long!.

Source: https://stackoverflow.com/questions/6461074/linking-error-for-unit-testing-with-xcode-4

Objective C Solutions


Solution 1 - Objective C

>> Class Foo is implemented in both MyApp and MyAppTestCase. One of the two will be used. Which one is undefined.

> I wonder why is that?

because both images (the app and the unit test bundle) define the implementation of the class. the class is dynamically loaded into the objc runtime. the objc runtime uses a flat namespace. how this works:

  • the binary is loaded, starting with its dependencies
  • as each binary is loaded, the objc classes register with the objc runtime
  • if a class with a specific name is loaded twice, the behaviour is undefined. one implementation of a class (with identical names) can be loaded into the objc runtime.

the typical problem here is that you will be returned one implementation - your app will likely crash when the type conflicts (when the class does not come from the same source file).

you typically avoid this by either renaming a class, or export the class in one image. renaming the class obviously does not apply to your case. you have one file Foo.m which is being compiled, exported, and loaded by two images when it should be in one.

this should be interpreted by you as a duplicate symbol linker error. even though the implementation is the same source file (and the implementation is the same) - this a problem that you must fix.

> How can I solve this?

if Foo.m is a class of the app, you have to remove (do not compile and link) Foo.m from the unit test. if it's part of the unit test, then do not compile and link it into the unit test target.

then, follow the instructions in the post for linking/loading your unit test to the app. it's in this general area of the post: where "WhereIsMyMac" is the name of the application you're unit testing. This will let the testing target link against the application (so you don't get linker errors when compiling). the important part is that your test files are compiled in the unit test target (only), and your app's classes are compiled and linked into the app. you can't just add them - they link and load dynamically.

> Maybe I missed something when setting the unit test target?

From the article you linked:

> > Note: The testing target is a separate target. This means that you need to be careful of target membership. All application source files should be added to the application target only. Test code files should be added to the testing target only.

the part that you got wrong is probably the link and load phases of the unit test bundle.

Solution 2 - Objective C

If you are using Cocoapods, your podfile only needs the dependencies in the section for the main target, not the test targets. If you do add duplicate dependencies for the test targets, you'll get the OP's error message.

target 'MyProject' do
pod 'Parse'
    
end

target 'MyProjectTests' do

end

target 'MyProjectUITests' do

end

Solution 3 - Objective C

For me, all I needed to do was uncheck the checkbox that makes the Foo class a member of the unit test target. It should not be a member of both targets, and should look like this:

Target Membership

In case you can't see the image, it's a screenshot of the Xcode "Target Membership" pane. There are two targets: one with an "A" application icon and the test name. The other is the unit test target, and has a unit test icon:

Target Membership
[X] Foo
[ ] FooTests

Solution 4 - Objective C

For me this happened because I deployed to the device and then to the simulator as I have NSZombies enabled. The solution was to switch to the simulator configuration & do a Product -> Clean then switch to the device configuration & do the same. Error went away. It's to do with build cache.

Solution 5 - Objective C

The reason is that you override RUNPATH_SEARCH_PATHS of your App Target`s build setting defined in other target.

Solution:

>Go to your App Target and find RUNPATH_SEARCH_PATHS build setting and use there $(inherited) flag for both: Debug and Release

Solution 6 - Objective C

Come across the same issues, My situation is Class NSNotification is implemented in both /System/Library/Frameworks/Foundation.framework/Foundation, is there any dude come across the same issue, any direction or advise will be appriciated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnacho4dView Question on Stackoverflow
Solution 1 - Objective CjustinView Answer on Stackoverflow
Solution 2 - Objective CRichardView Answer on Stackoverflow
Solution 3 - Objective CSteve HHHView Answer on Stackoverflow
Solution 4 - Objective CGeoff HView Answer on Stackoverflow
Solution 5 - Objective CBartłomiej SemańczykView Answer on Stackoverflow
Solution 6 - Objective Csuxinde2009View Answer on Stackoverflow