Xcode Unit Testing with Cocoapods

别等时光非礼了梦想. 提交于 2019-11-28 16:16:36

I had the same issue. I solved it by moving pod 'Firebase' to my test target. Change your Podfile to this:

platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!

target 'MyApp' do
    pod 'Firebase/Auth'
    pod 'Firebase/Database'
    pod 'Firebase/Storage'

    target 'MyAppTests' do
        inherit! :search_paths
        pod 'Firebase'
    end
end

Try changing the inheritance to :complete, as in:

target 'MyAppTests' do
    inherit! :complete
end

Importantly it allows anyone else checking out your repo to just do a pod update as usual without having to copy .xcconfig files or other hackery just to build.

  1. Select your Unit Test Target setting.
  2. Go to Build Settings.
  3. Look for Header Search Paths.
  4. Add this value $(SRCROOT)/Pods with recursive, then Xcode will resolve the path for you.

Example

Will

The issue is that Firebase does something special with the Header Search Paths after CocoaPods generates its own value for the setting so CocoaPods doesn't pick up on this change in order to carry it over to the test target. You can solve this one of two ways:

  1. Locate MyAppTests.<configuration>.xcconfig in the file navigator and add the following to HEADER_SEARCH_PATHS:

    ${PODS_ROOT}/Firebase/Analytics/Sources [*]

  2. Find the setting for Header Search Paths in Build Settings and add that same value as in option 1 to the list. You shouldn't need to set it as recursive.

* As per AKM's comment, this changed to ${PODS_ROOT}/Firebase/Core/Sources in version 3.14.0

The problem is recorded in the firebase project here:

https://github.com/firebase/firebase-ios-sdk/issues/58

There is a workaround:

Add "${PODS_ROOT}/Firebase/Core/Sources" to your Tests target only under Build Settings -> Header Search Paths

but this is also fixed by upgrading to CocoaPods 1.4.0 or later, which is a better solution.

At the time I'm writing this (November 2017) cocoapods 1.4.0 is still in beta, so to install it you need to explicitly request the beta:

gem install cocoapods --pre

This and then doing a pod install solved the problem running my tests.

Three Steps before I could get this to work:

CocoaPods : 1.5.0 Swift 4 Firebase : 4.13.0

Step 1: Make sure to add the following target block into your podfile.

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

target 'TIMII' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for TIMII
pod 'Firebase/Core'
pod 'Firebase/Database'
pod 'Firebase/Auth'
pod 'Firebase/Storage'

    target 'TIMIITests' do
        inherit! :search_paths
        pod 'Firebase/Core'
    end

end

Step 2: Within the YourAppTests Project Navigator Build Settings tab. Find the Header Search Path row and add to Debug the following line

$(inherited) ${PODS_ROOT}/Firebase/Core/Sources

Step 3: In terminal run:

pod update

The solution for me was to update cocoapods to version 1.1.0.rc.2.

sudo gem install cocoapods --pre

I tried all the above and ran into various different errors, originally starting with Missing required module 'Firebase', then getting "Class ... is implemented in both ... " or linker issues if I tried to add Firebase Pods to my test target.

The solution that worked for me was to:

  1. Remove test target entirely from Podfile and run 'pod update' to ensure the XCode project is in sync.
  2. Open my test target's Build Settings and update header search paths to only include the following 3 items:
    • $(inherited) non-recursive
    • $(SRCROOT)/Pods/Headers/Public recursive
    • $(SRCROOT)/Pods/Firebase recursive

At this point cleaning the build folder, re-building then re-running the tests worked for me. Hope this helps someone!

I had a similar problem. Phrased in terms of your question, I copied the contents of my MyApp.<configuration>.xcconfig file to my MyAppTests.<configuration>.xcconfig file. I cleaned and built the tests, and it worked.

Add "${PODS_ROOT}/Firebase/Core/Sources" to your Tests target only under Build Settings -> Header Search Paths

As @Will mentioned an issue around Header Search Paths after CocoaPods installation.

I have a project with multiple targets where pod 'Firebase' embedded into separate module, lets say MyProject-Shared. Firebase pod at 'Podfile' installed only for 'MyProject-Shared' target. Other modules, which wants to use 'MyProject-Shared' can't be compiled due an error:

'missing required module "Firebase" '

The trick in my case was to add following missing header search path at each target's Build Settings referencing to Analytics-Framework:

"${PODS_ROOT}/Firebase/CoreOnly/Sources"

Please see pic below:

Hope it will save your time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!