在 iOS 逻辑测试中使用 CocoaPods 时找不到库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14512792/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:00:18  来源:igfitidea点击:

Libraries not found when using CocoaPods with iOS logic tests

iosunit-testingcocoapods

提问by Mark Struzinski

I am trying to write some iOS logic tests against classes in my project that use functionality from some of the libraries in my podspec. I am using the standard unit test bundle provided in Xcode (although not Application Tests, just Unit Tests).

我正在尝试针对我的项目中的类编写一些 iOS 逻辑测试,这些类使用了我的 podspec 中某些库的功能。我正在使用 Xcode 中提供的标准单元测试包(虽然不是应用程序测试,只是单元测试)。

For example, I use Magical Record, and I have that library linked in my podspec. It is present in the Pods project in my workspace, and works as expected when the app is running in the simulator or on the device. When I try to link to the test the object that uses Magical Record, however, I get a linker error stating that it can't find the selectors from Magical Record. I have tried updating my HEADER_SEARCH_PATH in my logic testing bundle, even hard coding it to the headers directory created by CocoaPods, but no luck.

例如,我使用 Magical Record,并且在我的 podspec 中链接了该库。它存在于我工作区的 Pods 项目中,并且在应用程序在模拟器或设备上运行时按预期工作。但是,当我尝试将使用 Magical Record 的对象链接到测试时,出现链接器错误,指出它无法从 Magical Record 找到选择器。我已经尝试在我的逻辑测试包中更新我的 HEADER_SEARCH_PATH,甚至将它硬编码到 CocoaPods 创建的 headers 目录,但没有运气。

I can run unit tests against classes that do not use CocoaPods libraries with no problem.

我可以毫无问题地针对不使用 CocoaPods 库的类运行单元测试。

Am I going about this wrong? Should I be doing something else to get the compiler to see the CocoaPods libraries?

我这样做是错误的吗?我应该做其他事情让编译器看到 CocoaPods 库吗?

回答by Keith Smiley

CocoaPods 1.0 has changed the syntax for this. It now looks like this:

CocoaPods 1.0 为此更改了语法。现在看起来像这样:

def shared_pods
    pod 'SSKeychain', '~> 0.1.4'
    ...
end

target 'Sail' do
    shared_pods
end

target 'Sail-iOS' do
    shared_pods
end


Pre CocoaPods 1.0 answer

前 CocoaPods 1.0 答案

What you want to use is link_withfrom your Podfile. Something like:

您要使用的是link_with来自您的Podfile. 就像是:

link_with 'MainTarget', 'MainTargetTests'

Then run pod installagain.

然后pod install再次运行。

回答by Mark Struzinski

I figured this one out by looking at how the main target of my app was receiving settings from the CocoaPods library. CocoaPods includes an .xcconfig file named Pods.xcconfig. This file contains all of the header search paths.

我通过查看我的应用程序的主要目标如何从 CocoaPods 库接收设置来解决这个问题。CocoaPods 包含一个名为 Pods.xcconfig 的 .xcconfig 文件。此文件包含所有标头搜索路径。

If you look at your project in the project navigator and click the Info tab, you will see your build configurations listed on the top section. If you open the disclosure triangle for your different configurations, you will see Pods listed under your main target. I had to click the drop down and add Pods to the logic test target as well.

如果您在项目导航器中查看您的项目并单击信息选项卡,您将看到您的构建配置列在顶部部分。如果您打开不同配置的显示三角形,您将看到主要目标下列出的 Pod。我还必须单击下拉菜单并将 Pod 添加到逻辑测试目标。

Configurations Snapshot

配置快照

I also had to copy the settings of $(inherited)and ${PODS_HEADERS_SEARCH_PATHS}from my main target and copy them over to the logic test target under Build Settings/HEADER_SEARCH_PATHS.

我也有复制的设置$(inherited),并${PODS_HEADERS_SEARCH_PATHS}从我的主要目标,并在构建设置下将它们复制到逻辑测试目标/ HEADER_SEARCH_PATHS。

Finally, I had to add libPods.a in the Link Binary with Libraries build phase for my logic tests target.

最后,我必须在 Link Binary with Libraries 构建阶段为我的逻辑测试目标添加 libPods.a。

Hope this is able to help someone else.

希望这能帮助别人。

回答by Mingming

There is a solution I found here Unit Tests With CocoaPods:

我在这里找到了一个解决方案,使用 CocoaPods 进行单元测试

Open the project file in Xcode, then choose the Project (not the target), in the right panel, there is a section called Configurations. Choose Pods in the "Based on Configuration file" column for your test target.

在 Xcode 中打开项目文件,然后选择 Project(不是目标),在右侧面板中,有一个名为 Configurations 的部分。在“基于配置文件”列中为您的测试目标选择 Pod。

enter image description here

在此处输入图片说明

回答by JRV

I agree with the other answers telling that it is necessary to get the libraries linked to the test targets. However none of the suggestions so far helped me. As @fabb writes in a comment: "when testing, isSubclassOfClass:calls return NO where they should return YES. The only reason I can explain this is that the dependencies really get linked to both the main and the test target, and when the test target's bundle loader loads the main bundle, it cannot decide which class to take." I get the same problem with all the previous suggestions in this thread.

我同意其他答案,即有必要将库链接到测试目标。然而,到目前为止,没有任何建议对我有帮助。正如@fabb 在评论中所写:“在测试时,isSubclassOfClass:调用返回 NO,他们应该返回 YES。我可以解释这一点的唯一原因是依赖项确实链接到主目标和测试目标,并且当测试目标的包loader 加载主包,它无法决定采用哪个类。” 我在这个线程中的所有以前的建议中遇到了同样的问题。

The solution that I got to work was to update my Podfile to define specific Pods for my main target and my test target:

我开始工作的解决方案是更新我的 Podfile 为我的主要目标和我的测试目标定义特定的 Pod:

target 'MyTarget' do
   pod 'AFNetworking', '~> 2.5.0'
   pod 'Mantle', '~> 1.5'
end

target 'MyTargetTests' do
   pod 'OCMockito', '~> 1.3.1'
end

It was necessaryto specify a Pod for my test target even though I did not use any test specific Pods. Otherwise CocoaPods would not insert the necessary linking logic in my project.

即使我没有使用任何特定于测试的 Pod ,也有必要为我的测试目标指定一个 Pod。否则 CocoaPods 不会在我的项目中插入必要的链接逻辑。

This linkis what helped me come to this conclusion.

这个链接帮助我得出这个结论。

回答by Hai Feng Kao

I added :exclusive => trueto avoid duplicated symbol errors in the application test target.

我添加:exclusive => true以避免在应用程序测试目标中出现重复的符号错误。

target 'myProjectTests', :exclusive => true do
   pod 'OCMock', :head
   pod 'XCTAsyncTestCase', :git => 'https://github.com/iheartradio/xctest-additions.git'
end

link_with 'myProject', 'myProjectTests'

When I changed the application test target to the logic unit test one, the linker error occurs. After I remove :exclusive => true, everything works again.

当我将应用程序测试目标更改为逻辑单元测试目标时,发生链接器错误。我删除后:exclusive => true,一切又恢复了。

target 'myProjectTests', do
   pod 'OCMock', :head
   pod 'XCTAsyncTestCase', :git => 'https://github.com/iheartradio/xctest-additions.git'
end

link_with 'myProject', 'myProjectTests'

:exclusive => truestates that everything outside do...endshould NOT be linked to myProjectTests, which is reasonable in application test targets, but it will cause linker errors in logic test targets.

:exclusive => true声明外部的所有内容do...end都不应链接到myProjectTests,这在应用程序测试目标中是合理的,但它会导致逻辑测试目标中的链接器错误。

回答by Elihay

You can use link_with according to @Keith Smiley solution.

您可以根据@Keith Smiley 解决方案使用 link_with。

In case you have common pods, and specifics for each target, you might want to use the "def" option to define group of pods. and use the "def" later in exclusive target.

如果您有共同的 pod,以及每个目标的具体细节,您可能希望使用“def”选项来定义 pod 组。并稍后在独占目标中使用“def”。

def import_pods
    pod 'SSKeychain'
end

target 'MyProjectTests', :exclusive => true do
  import_pods
end

target 'MyProject', :exclusive => true do
  import_pods
  pod 'Typhoon'
end

in the example above, I added 'SSKeychain' to the both targets, and 'Typhoon' only to 'MyProject' target

在上面的示例中,我将“SSKeychain”添加到两个目标,“Typhoon”仅添加到“MyProject”目标

回答by Qw4z1

My solution to this problem was to change my Podfile to include the library in both targets like this

我对这个问题的解决方案是更改我的 Podfile 以将库包含在这样的两个目标中

target "MyApp" do  
    pod 'GRMustache', '~> 7.0.2'
end

target "MyAppTests" do
    pod 'GRMustache', '~> 7.0.2'
end

And since I'm using swift I also had to configure the test target to include the MyApp-Bridging-Header.hfile. (In the Swift Compiler group under the Build Settings tab)

由于我使用的是 swift,因此我还必须配置测试目标以包含该MyApp-Bridging-Header.h文件。(在 Build Settings 选项卡下的 Swift Compiler 组中)

回答by Maxwell

I had a similar occurrence when I lost some library files during some version control. I still saw the library file in my Pods but with the actual code missing, XCode said it was gone. To my dismay, running 'pod install' wasn't immediately bringing the lost files back.

当我在某些版本控制期间丢失了一些库文件时,我也发生了类似的情况。我仍然在我的 Pod 中看到了库文件,但实际代码丢失了,XCode 说它不见了。令我沮丧的是,运行“pod install”并没有立即将丢失的文件带回来。

I had to remove and replace the pod manually by doing the following:

我必须通过执行以下操作手动移除和更换吊舱:

  1. Remove the library from the Podfile
  2. Run 'pod install' to remove the library completely
  3. Put the library back into the Podfile
  4. Run 'pod install' again
  1. 从 Podfile 中删除库
  2. 运行“pod install”以完全删除库
  3. 将库放回 Podfile
  4. 再次运行“pod install”

This should put the library in question back in it's original form.

这应该使有问题的图书馆恢复其原始形式。

回答by Darren Black

As of CocoaPods 1.x, there's a new way to declare shared dependencies between a target and the corresponding test target. I'd been using the accepted solution by Mark Struzinski to this point, but using this method yielded a massive number of warnings when running my tests that:

从 CocoaPods 1.x 开始,有一种新方法可以声明目标和相应测试目标之间的共享依赖项。到目前为止,我一直在使用 Mark Struzinski 接受的解决方案,但是在运行我的测试时使用这种方法会产生大量警告:

Class SomeClass is implemented in both /Path/To/Test/Target and /Path/To/App/Target. One of the two will be used. Which one is undefined.

With CocoaPods 1.x we can declare our -Test target as inheriting via the parent target's search paths, like so:

在 CocoaPods 1.x 中,我们可以将 -Test 目标声明为通过父目标的搜索路径继承,如下所示:

target 'MyApp' do
    pod 'aPod'
    pod 'anotherPod'
    project 'MyApp.xcodeproj'
end
target 'MyAppTests' do
    inherit! :search_paths
    project 'MyApp.xcodeproj'
end

This will result in the -Test target having access to the dependencies of the app target, without multiple binary copies. This has seriously sped up test build times for me.

这将导致 -Test 目标可以访问应用程序目标的依赖项,而无需多个二进制副本。这大大加快了我的测试构建时间。

回答by Jaywant Khedkar

Try This it's working for me ,

试试这对我有用,

We need to set Pods in Configurations ,

我们需要在 Configurations 中设置 Pods ,

The Project->Info->Configurationsin the Xcode project (your project) should be set to main project 'Pods' for Debug, Release (and what else you have). See "Headers not found – search paths not included"

Xcode 项目(您的项目)中的 Project->Info->Configurations应设置为主项目“Pods”以进行调试、发布(以及您拥有的其他内容)。请参阅“未找到标题 - 不包括搜索路径”

enter image description here

在此处输入图片说明

Hope this is help to some one .

希望这对某人有所帮助。