ios 使用 Cocoapods 进行 Xcode 单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38216090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode Unit Testing with Cocoapods
提问by doovers
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!
在过去的几天里,我一直在用这个头撞墙,但是尽管进行了多次 Google/SO/Github 搜索,我还是找不到解决我遇到的问题的方法!
All I'm trying to do is create some unit tests for my app which makes use of Firebase pods.
我要做的就是为我的应用程序创建一些单元测试,它使用 Firebase pod。
I'm using Xcode 7.3.1 & Cocoapods 1.0.1. Update:Issue remains with Xcode 8.0
我正在使用 Xcode 7.3.1 和 Cocoapods 1.0.1。更新:问题仍然存在于 Xcode 8.0
With this podfile:
使用这个 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
在我的 XCTest 课上,我得到
Missing required module 'Firebase'
缺少必需的模块“Firebase”
error at @testable import MyApp
错误在 @testable import MyApp
Alternatively with this podfile:
或者使用这个 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
Class <-FirebaseClassName-> 在 ...MyApp... 和 ...MyAppTests... 中都实现了...将使用两者之一。哪个是未定义的
采纳答案by doovers
The solution for me was to update cocoapods to version 1.1.0.rc.2.
我的解决方案是将 cocoapods 更新到 1.1.0.rc.2 版本。
sudo gem install cocoapods --pre
sudo gem install cocoapods --pre
回答by Mackarous
I had the same issue. I solved it by moving pod 'Firebase'
to my test target. Change your Podfile to this:
我遇到过同样的问题。我通过移动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
回答by Andy Chou
Try changing the inheritance to :complete
, as in:
尝试将继承更改为:complete
,如下所示:
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.
重要的是,它允许其他任何人检查你的 repopod update
像往常一样做,而不必复制.xcconfig
文件或其他黑客只是为了构建。
回答by Kanika Sharma
回答by 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:
问题在于,在 CocoaPods 为设置生成自己的值后,Firebase 对 Header Search Paths 做了一些特别的事情,因此 CocoaPods 不会接受此更改以将其转移到测试目标。您可以通过以下两种方式之一解决此问题:
Locate
MyAppTests.<configuration>.xcconfig
in the file navigator and add the following toHEADER_SEARCH_PATHS
:${PODS_ROOT}/Firebase/Analytics/Sources
[*]Find the setting for Header Search Pathsin Build Settings and add that same value as in option 1 to the list. You shouldn't need to set it as recursive.
MyAppTests.<configuration>.xcconfig
在文件导航器中找到并将以下内容添加到HEADER_SEARCH_PATHS
:${PODS_ROOT}/Firebase/Analytics/Sources
[*]在 Build Settings 中找到Header Search Paths的设置,并将与选项 1 中相同的值添加到列表中。您不需要将其设置为递归。
* As per AKM's comment, this changed to ${PODS_ROOT}/Firebase/Core/Sources
in version 3.14.0
* 根据 AKM 的评论,这${PODS_ROOT}/Firebase/Core/Sources
在 3.14.0 版中更改为
回答by JosephH
The problem is recorded in the firebase project here:
问题记录在这里的firebase项目中:
https://github.com/firebase/firebase-ios-sdk/issues/58
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
仅在 Build Settings -> Header Search Paths 下将“${PODS_ROOT}/Firebase/Core/Sources”添加到您的测试目标
but this is also fixed by upgrading to CocoaPods 1.4.0 or later, which is a better solution.
但这也可以通过升级到 CocoaPods 1.4.0 或更高版本来解决,这是一个更好的解决方案。
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:
在我撰写本文时(2017 年 11 月),cocoapods 1.4.0 仍处于测试阶段,因此要安装它,您需要明确请求测试版:
gem install cocoapods --pre
This and then doing a pod install
solved the problem running my tests.
这然后做一个pod install
解决了运行我的测试的问题。
回答by Sid
Three Steps before I could get this to work:
在我让它工作之前的三个步骤:
CocoaPods : 1.5.0 Swift 4 Firebase : 4.13.0
CocoaPods:1.5.0 Swift 4 Firebase:4.13.0
Step 1: Make sure to add the following target block into your podfile.
第 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
Step 2: Within the YourAppTests Project Navigator Build Settings tab. Find the Header Search Path row and add to Debug the following line
第 2 步:在 YourAppTests 项目导航器构建设置选项卡中。找到 Header Search Path 行并将以下行添加到 Debug
$(inherited) ${PODS_ROOT}/Firebase/Core/Sources
$(继承)${PODS_ROOT}/Firebase/Core/Sources
Step 3: In terminal run:
第 3 步:在终端运行中:
pod update
豆荚更新
回答by Mike Atkins-Spelling
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.
我尝试了上述所有方法并遇到了各种不同的错误,最初以 开头Missing required module 'Firebase'
,然后"Class ... is implemented in both ... "
如果我尝试将 Firebase Pod 添加到我的测试目标,则会出现获取或链接器问题。
The solution that worked for me was to:
对我有用的解决方案是:
- Remove test target entirely from Podfile and run 'pod update' to ensure the XCode project is in sync.
- 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
- 从 Podfile 中完全删除测试目标并运行“pod update”以确保 XCode 项目同步。
- 打开我的测试目标的构建设置并更新标题搜索路径以仅包含以下 3 项:
$(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!
此时清理构建文件夹,重新构建然后重新运行对我有用的测试。希望这可以帮助某人!
回答by User123335511231
Adding ${SRCROOT}/Pods/Firebase/CoreOnly/Sources
into the unit test target's "Header search paths" fixed the problem.
Steps:
添加${SRCROOT}/Pods/Firebase/CoreOnly/Sources
到单元测试目标的“标题搜索路径”中解决了这个问题。脚步:
- Select your unit tests target
- Go to Build Settings
- Search for header search path
- Add ${SRCROOT}/Pods/Firebase/CoreOnly/Sources
- 选择您的单元测试目标
- 转到构建设置
- 搜索标题搜索路径
- 添加${SRCROOT}/Pods/Firebase/CoreOnly/Sources
After this the tests can run and the error will disappear.
在此之后,测试可以运行并且错误将消失。
回答by Jason
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.
我有一个类似的问题。就您的问题而言,我将MyApp.<configuration>.xcconfig
文件的内容复制到我的MyAppTests.<configuration>.xcconfig
文件中。我清理并构建了测试,它奏效了。