首页 文章

使用Cocoapods进行Xcode单元测试

提问于
浏览
75

在过去的几天里,我一直在用头撞墙,但尽管有多次Google / SO / Github搜索,我找不到解决我遇到的问题的方法!

我要做的就是为我的应用程序创建一些使用Firebase pod的单元测试 .

我正在使用Xcode 7.3.1和Cocoapods 1.0.1 . Update: 问题仍然存在于Xcode 8.0中

使用此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

在我的XCTest课程中,我得到了

缺少必需的模块'Firebase'

错误在 @testable import MyApp

或者使用此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

测试构建,但我的控制台充满了警告,例如:

Class <-FirebaseClassName->在...... MyApp ...和... MyAppTests中实现......将使用其中一个 . 哪一个未定义

10 回答

  • 1

    尝试将继承更改为 :complete ,如下所示:

    target 'MyAppTests' do
        inherit! :complete
    end
    

    重要的是,它允许任何其他人检查你的回购只是像往常一样 pod update 而不必复制 .xcconfig 文件或其他hackery只是为了构建 .

  • 3

    仅在 Build Settings - > Header Search Paths 下将 "$/Firebase/Core/Sources" 添加到测试目标

  • 9

    我有类似的问题 . 根据您的问题,我将 MyApp.<configuration>.xcconfig 文件的内容复制到我的 MyAppTests.<configuration>.xcconfig 文件中 . 我清理并构建了测试,并且工作正常 .

  • 2

    问题是,在CocoaPods为设置生成自己的值之后,Firebase会对标头搜索路径执行一些特殊操作,因此CocoaPods不会接受此更改以将其传送到测试目标 . 您可以通过以下两种方式解决此问题:

    • 在文件导航器中找到 MyAppTests.<configuration>.xcconfig 并将以下内容添加到 HEADER_SEARCH_PATHS

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

    • 在构建设置中查找 Header Search Paths 的设置,并将与选项1中相同的值添加到列表中 . 您不应该将其设置为递归 .

    *根据AKM的评论,这在版本3.14.0中更改为$ / Firebase / Core / Sources

  • 32
    • 选择单位测试目标设置 .

    • 转到“构建设置” .

    • 寻找 Headers 搜索路径 .

    • 使用递归添加此值$(SRCROOT)/ Pods,然后Xcode将为您解析路径 .

    Example

  • 33

    我的解决方案是将cocoapods更新到1.1.0.rc.2版 .

    sudo gem install cocoapods --pre

  • 1

    在我开始工作之前的三个步骤:

    CocoaPods:1.5.0 Swift 4 Firebase:4.13.0

    步骤1:确保将以下目标块添加到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
    

    第2步:在YourAppTests项目导航器构建设置选项卡中 . 找到 Headers 搜索路径行并添加到调试以下行

    $(继承)$ / Firebase /核心/来源

    第3步:在终端运行中:

    pod更新

  • 6

    这个问题记录在firebase项目中:

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

    有一个解决方法:

    仅在“构建设置” - >“ Headers 搜索路径”下的“测试”目标中添加“$ / Firebase / Core / Sources”

    但这也是通过升级到CocoaPods 1.4.0或更高版本来解决的,这是一个更好的解决方案 .

    当我写这篇文章(2017年11月)时,cocoapods 1.4.0仍处于测试阶段,因此要安装它,您需要明确请求测试版:

    gem install cocoapods --pre
    

    这个然后做 pod install 解决了运行我的测试的问题 .

  • 58

    我遇到过同样的问题 . 我通过将 pod 'Firebase' 移动到我的测试目标来解决它 . 将您的Podfile更改为:

    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
    
  • 3

    正如@Will在CocoaPods安装后提到 Header Search Paths 周围的问题 .

    我有一个项目有多个目标,其中pod 'Firebase'嵌入到单独的模块中,让我们说 MyProject-Shared . 'Podfile'的Firebase pod仅为'MyProject-Shared'目标安装 . 由于错误,无法编译其他想要使用'MyProject-Shared'的模块:

    '缺少必需的模块“Firebase”'

    在我的情况下,诀窍是在 each target的 Build Settings 引用Analytics-Framework时添加以下缺少的 Headers 搜索路径:

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

    请参见下图:
    Header Search Path

    希望它能节省您的时间 .

相关问题