ChatGPT解决这个技术问题 Extra ChatGPT

Libraries not found when using CocoaPods with iOS logic tests

I am trying to write some iOS logic tests against classes in my project that use functionality from some of the libraries in my podspec. I am using the standard unit test bundle provided in Xcode (although not Application Tests, just Unit Tests).

For example, I use Magical Record, and I have that library linked in my podspec. It is present in the Pods project in my workspace, and works as expected when the app is running in the simulator or on the device. When I try to link to the test the object that uses Magical Record, however, I get a linker error stating that it can't find the selectors from Magical Record. I have tried updating my HEADER_SEARCH_PATH in my logic testing bundle, even hard coding it to the headers directory created by CocoaPods, but no luck.

I can run unit tests against classes that do not use CocoaPods libraries with no problem.

Am I going about this wrong? Should I be doing something else to get the compiler to see the CocoaPods libraries?


C
Cœur

CocoaPods 1.0 has changed the syntax for this. It now looks like this:

def shared_pods
    pod 'SSKeychain', '~> 0.1.4'
    ...
end

target 'Sail' do
    shared_pods
end

target 'Sail-iOS' do
    shared_pods
end

Pre CocoaPods 1.0 answer

What you want to use is link_with from your Podfile. Something like:

link_with 'MainTarget', 'MainTargetTests'

Then run pod install again.


This immediately fixed up the problem for me.
I get strange errors with this - when testing, isSubclassOfClass: calls return NO where they should return YES. The only reason I can explain this is that the dependencies really get linked to both the main and the test target, and when the test target's bundle loader loads the main bundle, it cannot decide which class to take.
I have the same issue with isKindOfClass: returning NO when it should return YES. If I log the pointer to the Class of my object I'm testing and the Class of the class I want to compare against they are two different values. Clearly my code from the app bundle is using a different symbol for the class than the code from my unit tests. Has anyone found a way to resolve this?
I don't think this is a good way to go due to the errors some others have mentioned. Stick with updating the configuration file 'based on' bit. Make sure you haven't linked libPods.a twice.
This should be the accepted answer since this is the official CocoaPods way to setup Pods with multiple targets. Thx a lot Keith!
M
Mark Struzinski

I figured this one out by looking at how the main target of my app was receiving settings from the CocoaPods library. CocoaPods includes an .xcconfig file named Pods.xcconfig. This file contains all of the header search paths.

If you look at your project in the project navigator and click the Info tab, you will see your build configurations listed on the top section. If you open the disclosure triangle for your different configurations, you will see Pods listed under your main target. I had to click the drop down and add Pods to the logic test target as well.

https://i.stack.imgur.com/LNkZN.png

I also had to copy the settings of $(inherited) and ${PODS_HEADERS_SEARCH_PATHS} from my main target and copy them over to the logic test target under Build Settings/HEADER_SEARCH_PATHS.

Finally, I had to add libPods.a in the Link Binary with Libraries build phase for my logic tests target.

Hope this is able to help someone else.


Brilliant! I use MagicalRecord and also OCMockito and OCHamcrest for unit testing. With this fix I can now install them all through CocoaPods! Thanks!
This worked for me, thanks. NOTE.. I didn't need to add the libPods.a into both the test proj and main proj. This causes a duplicate symbol error
For me, I also had to copy the "User-Defined" build settings. The Header Search Paths refer to $PODS_ROOT which was not defined on the test target. You can add it by going to Editor->Add Build Setting->Add User-Defined Setting then copying the $PODS_ROOT value from the main target.
This is not the correct way to fix this. See answer with link_with. You can also specify different pods on a per target basis in your pod file, i.e., only include OCMockito in your test target.
Yes, yes, yes! Before this answer I had to delete Test target from my projects! Thanks man :)
M
Mingming

There is a solution I found here Unit Tests With CocoaPods:

Open the project file in Xcode, then choose the Project (not the target), in the right panel, there is a section called Configurations. Choose Pods in the "Based on Configuration file" column for your test target.

https://i.stack.imgur.com/wvdyA.png


Well, what if there are test-specific dependencies, like Specta that you want to link with the test project but not with the main project? :S
This worked and doesn't require any changes to the pod config or setup... Excellent solution.
Although this solution may create an error: Class Foo is implemented in both MyApp and MyAppTestCase. One of the two will be used. Which one is undefined. This seems to be caused by a bug in Cocoapods; see @JRV answer below.
Those are not just warnings. With such a setup no proper Xcode code coverage data is generated and unit tests just hang during launch in most cases.
I have imported the Estimote SDK manually by drag and drop, I am not getting pods. How to resolve this?
J
JRV

I agree with the other answers telling that it is necessary to get the libraries linked to the test targets. However none of the suggestions so far helped me. As @fabb writes in a comment: "when testing, isSubclassOfClass: calls return NO where they should return YES. The only reason I can explain this is that the dependencies really get linked to both the main and the test target, and when the test target's bundle loader loads the main bundle, it cannot decide which class to take." I get the same problem with all the previous suggestions in this thread.

The solution that I got to work was to update my Podfile to define specific Pods for my main target and my test target:

target 'MyTarget' do
   pod 'AFNetworking', '~> 2.5.0'
   pod 'Mantle', '~> 1.5'
end

target 'MyTargetTests' do
   pod 'OCMockito', '~> 1.3.1'
end

It was necessary to specify a Pod for my test target even though I did not use any test specific Pods. Otherwise CocoaPods would not insert the necessary linking logic in my project.

This link is what helped me come to this conclusion.


Thanks for the link to the CocoaPods issue - that helped me solve my problem!
YES!!!! This issue has been plaguing me. This is the only sensible cocoapods answer I came across.
There's a better way of handling this under 1.x: stackoverflow.com/a/40866889/2799670
H
Hai Feng Kao

I added :exclusive => true to avoid duplicated symbol errors in the application test target.

target 'myProjectTests', :exclusive => true do
   pod 'OCMock', :head
   pod 'XCTAsyncTestCase', :git => 'https://github.com/iheartradio/xctest-additions.git'
end

link_with 'myProject', 'myProjectTests'

When I changed the application test target to the logic unit test one, the linker error occurs. After I remove :exclusive => true, everything works again.

target 'myProjectTests', do
   pod 'OCMock', :head
   pod 'XCTAsyncTestCase', :git => 'https://github.com/iheartradio/xctest-additions.git'
end

link_with 'myProject', 'myProjectTests'

:exclusive => true states that everything outside do...end should NOT be linked to myProjectTests, which is reasonable in application test targets, but it will cause linker errors in logic test targets.


Exclusive was the solution for me, as shown in kylef's answer on this CocoaPods issue , which was found thanks to JRV's answer on this question!
Yes, everyone should read that issue on github linked by @karlbecker_com. Seems this is just a long running limitation of cocoapods. According to the discussion there, link_with is not necessary. Simply add the test target and use :exclusive. If your test target doesn't need any specific pods, add one anyway otherwise cocoapods won't process it.
@kball Which one doesn't need link_with? The application test or the logic unit test?
Unless you have another reason to be using it, you shouldn't need link_with at all. And generally speaking you don't want to link those pods with your test bundle. They should only be linked once, in the app bundle, and then referenced by your tests through the dependency (ensuring Symbols Hidden by Default is off). Otherwise the behavior is undefined because two versions of the pods will exist - one included in the app target, one in the test target.
E
Elihay

You can use link_with according to @Keith Smiley solution.

In case you have common pods, and specifics for each target, you might want to use the "def" option to define group of pods. and use the "def" later in exclusive target.

def import_pods
    pod 'SSKeychain'
end

target 'MyProjectTests', :exclusive => true do
  import_pods
end

target 'MyProject', :exclusive => true do
  import_pods
  pod 'Typhoon'
end

in the example above, I added 'SSKeychain' to the both targets, and 'Typhoon' only to 'MyProject' target


Q
Qw4z1

My solution to this problem was to change my Podfile to include the library in both targets like this

target "MyApp" do  
    pod 'GRMustache', '~> 7.0.2'
end

target "MyAppTests" do
    pod 'GRMustache', '~> 7.0.2'
end

And since I'm using swift I also had to configure the test target to include the MyApp-Bridging-Header.h file. (In the Swift Compiler group under the Build Settings tab)


Careful — this will increase your build times by lots, as you keep adding more pods!
@fatuhoku didn't know that. Can you provide some insight as to why it increases build time?
Well each mention of a pod is a target in your Pods project. By mentioning your pods twice (once for tests and once for the app), you'll have two sets of targets. This effectively doubles the configuration work pod install has to do. This won't be an issue until you have > 15 pods though so don't worry too much until then.
This is the only solution that works for me with Cocoapods 1.0
As of 1.x, this is the official method for tests inheriting app dependencies: stackoverflow.com/a/40866889/2799670
M
Maxwell

I had a similar occurrence when I lost some library files during some version control. I still saw the library file in my Pods but with the actual code missing, XCode said it was gone. To my dismay, running 'pod install' wasn't immediately bringing the lost files back.

I had to remove and replace the pod manually by doing the following:

Remove the library from the Podfile Run 'pod install' to remove the library completely Put the library back into the Podfile Run 'pod install' again

This should put the library in question back in it's original form.


M
Mat Ryer

It's also worth noting that if you have libPods.a added twice, you'll get some nasty error like this:

232 duplicate symbols for architecture i386

To fix it, just delete one of the libPods.a references in your Project Explorer.


D
Darren Black

As of CocoaPods 1.x, there's a new way to declare shared dependencies between a target and the corresponding test target. I'd been using the accepted solution by Mark Struzinski to this point, but using this method yielded a massive number of warnings when running my tests that:

Class SomeClass is implemented in both /Path/To/Test/Target and /Path/To/App/Target. One of the two will be used. Which one is undefined.

With CocoaPods 1.x we can declare our -Test target as inheriting via the parent target's search paths, like so:

target 'MyApp' do
    pod 'aPod'
    pod 'anotherPod'
    project 'MyApp.xcodeproj'
end
target 'MyAppTests' do
    inherit! :search_paths
    project 'MyApp.xcodeproj'
end

This will result in the -Test target having access to the dependencies of the app target, without multiple binary copies. This has seriously sped up test build times for me.


S
Syed Zeeshan

Try This it's working for me ,

We need to set Pods in Configurations ,

The Project->Info->Configurations in the Xcode project (your project) should be set to main project 'Pods' for Debug, Release (and what else you have). See "Headers not found – search paths not included"

https://i.stack.imgur.com/3a5PH.png

Hope this is help to some one .


a
appledevguru

I am working with GoogleMaps Objective-C POD integration on iOS with my Swift app and so for me the issue was that the Test target didn't have a reference to the Bridge Header File (SWIFT_OBJC_BRIDGING_HEADER) in the Build Settings. Make sure both your app and test app targets point to that so that the 3rd party API calls (maps API, etc.,) can be used in swift unit tests.


I have a similar setup as you. I have already added the bridging header to the test target, However I get the error "No such module 'GoogleMaps' " on import GoogleMaps.
M
Maxim Kholyavkin

Next syntax gives best result for me (tested under cocoapod v.1.2.1):

https://github.com/CocoaPods/CocoaPods/issues/4626#issuecomment-210402349

 target 'App' do
    pod 'GoogleAnalytics' , '~> 3.0'
    pod 'GoogleTagManager' , '~> 3.0'

     pod 'SDWebImage', '~>3.7'
     platform :ios, '8.0'
     use_frameworks!

     target 'App Unit Tests' do
         inherit! :search_paths
     end
 end

Without this I have warnings while test run about duplicate symbols.

After this warnings were disappear.


F
Foti Dim

I had issues using OpenCV under XCTest. It was giving me linker errors of Undefined symbols for architecture arm64 for classes like cv::Mat. I am installing OpenCV through CocoaPods using pod 'OpenCV', '~> 2.0' under the main target. No matter how hard I tried to put the OpenCV dependency under the test target or use inherit! :search_paths none of it worked. The solution was to create an abstract_target like so:

# Uncomment the next line to define a global platform for your project
platform :ios, '6.1.6'

abstract_target 'Shows' do
  pod 'RMVision', path: '../..'
  pod 'RMShared', path: '../../../RMShared'
  pod 'OpenCV', '~> 2.0'

  target 'RMVisionSample' do
    # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
    # use_frameworks!

    # Pods for RMVisionSample
  end

  target 'RMVisionSampleTests' do
    # inherit! :search_paths
    # Pods for testing
  end

  target 'RMVisionBenchmarks' do
    # inherit! :search_paths
    # Pods for testing
  end

end 

Also useful are the pod deintegrate & pod clean commands which help to cleanup the project and make sure that you start fresh when testing. You can install those two using [sudo] gem install cocoapods-deintegrate cocoapods-clean.