Xcode Unit Testing with Cocoapods

I've been banging my head against a wall with this for the last few days but despite multiple Google/SO/Github searches I can't find a resolution to the issues I'm having!

All I'm trying to do is create some unit tests for my app which makes use of Firebase pods.

I'm using Xcode 7.3.1 & Cocoapods 1.0.1. Update: Issue remains with Xcode 8.0

With this podfile:

platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!

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

    target 'MyAppTests' do
        inherit! :search_paths
    end
end

In my XCTest class I get

Missing required module 'Firebase'

error at @testable import MyApp

Alternatively with this podfile:

platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!

def common_pods
    pod 'SwiftyTimer'
    pod 'Firebase'
    pod 'Firebase/Auth'
    pod 'Firebase/Database'
    pod 'Firebase/Storage'
end

target 'MyApp' do
    common_pods
end

target 'MyAppTests' do
    common_pods
end

The tests build but my console is littered with warnings e.g.:

Class <-FirebaseClassName-> is implemented in both ...MyApp... and ...MyAppTests... One of the two will be used. Which one is undefined


Solution 1:

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

Solution 2:

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.

Solution 3:

  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.

Here is Example

Solution 4:

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

Solution 5:

Adding ${SRCROOT}/Pods/Firebase/CoreOnly/Sources into the unit test target's "Header search paths" fixed the problem. Steps:

  1. Select your unit tests target
  2. Go to Build Settings
  3. Search for header search path
  4. Add ${SRCROOT}/Pods/Firebase/CoreOnly/Sources

enter image description here

After this the tests can run and the error will disappear.