使用Cocoapods进行Xcode单元测试

doo*_*ers 105 xcode unit-testing ios cocoapods xctest

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

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

我正在使用Xcode 7.3.1和Cocoapods 1.0.1.更新:问题仍然存在于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
Run Code Online (Sandbox Code Playgroud)

在我的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
Run Code Online (Sandbox Code Playgroud)

测试构建但我的控制台上到处都是警告,例如:

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

小智 77

我遇到过同样的问题.我通过移动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
Run Code Online (Sandbox Code Playgroud)

  • 进行此更改并未立即清除警告,但在运行`pod install`后它已解决. (3认同)

And*_*hou 55

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

target 'MyAppTests' do
    inherit! :complete
end
Run Code Online (Sandbox Code Playgroud)

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


Kan*_*rma 47

  1. 选择单元测试目标设置.
  2. 转到构建设置.
  3. 寻找标题搜索路径.
  4. 使用递归添加此值$(SRCROOT)/ Pods,然后Xcode将为您解析路径.

  • 简单的答案就为我完成了工作。 (2认同)

Wil*_*ill 33

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

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

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

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

*根据AKM的评论,这改为${PODS_ROOT}/Firebase/Core/Sources版本3.14.0

  • 从Firebase 3.14.0开始,此解决方案有效,但补丁更改为"$ {PODS_ROOT}/Firebase/Core/Sources" (8认同)

Dan*_*iel 9

更简单的方法也有效:

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

    target :MyAppTests
end
Run Code Online (Sandbox Code Playgroud)


Use*_*231 8

添加${SRCROOT}/Pods/Firebase/CoreOnly/Sources到单元测试目标的“标题搜索路径”中解决了这个问题。脚步:

  1. 选择您的单元测试目标
  2. 转到构建设置
  3. 搜索标题搜索路径
  4. 添加${SRCROOT}/Pods/Firebase/CoreOnly/Sources

在此处输入图片说明

在此之后,测试可以运行并且错误将消失。


Jos*_*phH 7

问题记录在firebase项目中:

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

有一个解决方法:

仅在构建设置 - >标题搜索路径下将"$ {PODS_ROOT}/Firebase/Core/Sources"添加到您的测试目标

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

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

gem install cocoapods --pre
Run Code Online (Sandbox Code Playgroud)

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


Sid*_*Sid 6

在使此工作之前,需要执行三个步骤:

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
Run Code Online (Sandbox Code Playgroud)

步骤2:在YourAppTests Project Navigator Build Settings标签中。找到“标题搜索路径”行,并添加到“调试”下一行

$(继承)$ {PODS_ROOT} / Firebase / Core / Sources

步骤3:在终端运行中:

吊舱更新


小智 5

我尝试了上述所有方法,但遇到了各种不同的错误,最初是从 开始Missing required module 'Firebase',然后"Class ... is implemented in both ... "如果我尝试将 Firebase Pods 添加到我的测试目标,则会出现或链接器问题。

对我有用的解决方案是:

  1. 从 Podfile 中完全删除测试目标并运行“pod update”以确保 XCode 项目同步。
  2. 打开我的测试目标的构建设置并更新标头搜索路径以仅包含以下 3 项:
    • $(inherited) non-recursive
    • $(SRCROOT)/Pods/Headers/Public recursive
    • $(SRCROOT)/Pods/Firebase recursive

此时清理构建文件夹,重新构建然后重新运行测试对我有用。希望这对某人有帮助!


doo*_*ers 4

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

sudo gem install cocoapods --pre