Xcode Unit Testing with Cocoapods

41,764

Solution 1

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

sudo gem install cocoapods --pre

Solution 2

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 3

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 4

  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 5

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

Share:
41,764
doovers
Author by

doovers

Mechanical Engineer turned full-stack software developer. Passionate about technology and all things programming. My work is also my hobby and the ability to face new challenges and learn new things on a daily basis is what drives me.

Updated on July 08, 2022

Comments

  • doovers
    doovers almost 2 years

    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

  • doovers
    doovers almost 8 years
    Thanks but I just tried that and I still have the "Class ... is implemented in both ... " issue.
  • doovers
    doovers over 7 years
    Tried that but still getting same warnings
  • Zeb
    Zeb over 7 years
    Unfortunately it doesn't work for me...I have to take back my upvote!
  • doovers
    doovers over 7 years
    Sorry it didn't help you out guys, not sure why it solved the problem for me and not you...
  • SauloT
    SauloT over 7 years
    This was the only solution that worked for me. Thanks! I'm thinking about do a post install processing in the Podfile doing this
  • Diogo T
    Diogo T over 7 years
    This also have the "Class ... is implemented in both ... " issue
  • AKM
    AKM about 7 years
    As of Firebase 3.14.0 This solution is valid, however the patch changed to${PODS_ROOT}/Firebase/Core/Sources
  • UKDataGeek
    UKDataGeek about 7 years
    I am on 1.20 and no luck
  • Dirty Henry
    Dirty Henry about 7 years
    It worked for me but it still doesn't make sense to me. It's not the 1st time I can tell Firebase are setting up their podspecs weirdly, cf. twitter.com/dirtyhenry/status/836240557311684608 but the Firebase team doesn't reply :(
  • Paulo Mattos
    Paulo Mattos about 7 years
    New path worked for me as well! I'm running CocoaPods version 1.2.0, but keep in mind I also had to add inherit! :search_paths to the test target in the Podfile.
  • Jonny
    Jonny almost 7 years
    2. does not work for me. Using Firebase 3.x (not the newest 4.x)
  • CMont
    CMont almost 7 years
    It made the compile time error disappear, but caused this one at runtime: clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • CMont
    CMont almost 7 years
  • blwinters
    blwinters over 6 years
    Making this change didn't immediately clear the warning, but it was resolved after I ran pod install.
  • smapps
    smapps over 6 years
    Worked for me as well! Thanks :)
  • ArgaPK
    ArgaPK over 6 years
    what is it "PODS_ROOT" ??
  • ArgaPK
    ArgaPK over 6 years
    What is it ? PODS_ROOT
  • JosephH
    JosephH over 6 years
    @ArgaPK It's a variable that cocoapods sets. Just paste exactly what I wrote between the ""s into Xcode. Really the best way is to upgrade your CocoaPods to >= 1.4.0.
  • Karthick Vadivel
    Karthick Vadivel over 6 years
    it will fetch directory for pod
  • ArgaPK
    ArgaPK over 6 years
    Half problem solved. and other half is solved by Add "${PODS_ROOT}/Firebase/Core/Sources" to your Tests target only under Build Settings -> Header Search Paths
  • ArgaPK
    ArgaPK over 6 years
    Half Problem solved and in other half include pod 'Firebase' in test target.
  • Stacy Smith
    Stacy Smith about 6 years
    @JosephH updating my cocoapods to 1.5.2 didn't solve the problem, unfortunately.
  • JosephH
    JosephH about 6 years
    @AnastasiaKovaleva did you run pod install after upgrading?
  • Stacy Smith
    Stacy Smith almost 6 years
    @JosephH Running pod install solved my problem! Thanks a lot!
  • Ali Parr
    Ali Parr almost 5 years
    After trying everything else on this thread - this was the only answer that got things working for me. Thank you!
  • jawad
    jawad over 4 years
    After you make this change do not forget to run pod install in order to update project dependencies ;)
  • Rahul K Rajan
    Rahul K Rajan over 4 years
    Thank you @Kanika Sharma
  • Saran
    Saran over 3 years
    This is what worked for me. No need to touch podfile or xcconfig. Thanks for sharing.
  • Tim Chen
    Tim Chen about 3 years
    This works perfectly. Thank you. Any documentation on the syntax? What does : MyAppTests mean?
  • Bruce
    Bruce about 3 years
    This works for me as well, and syntax looks the simplest!