xcode 无法将主要的 swift 类导入测试目标?

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

Can't import main swift classes into test target?

iosxcodeswiftimportxctest

提问by dcgoss

I am trying to test the classes in my iOS app. I am trying to import the target Picklein my app that has all my classes into my testing target PickleTestsby adding import Pickleto the top of my PickleTests.swifttesting file, but I keep getting an error.

The error I keep receiving is: "Failed to import bridging header (path to bridging header)" (the path to the bridging header is shown in the error, not the parentheses).

I have tried setting "Defines module" in my build settings for Pickletarget to "Yes", but it still doesn't work. I have also verified in the build settings that it has the correct path to the bridging header file.

Any ideas as to how I can set up my testing? I am on Xcode 6.3.2. Please let me know if you need any additional info.

我正在尝试测试我的 iOS 应用程序中的类。我试图通过添加到我的测试文件的顶部,将Pickle我的应用程序中包含我所有类的目标导入我的测试目标,但我一直收到错误消息。我一直收到的错误是:“无法导入桥接头(桥接头的路径)”(桥接头的路径显示在错误中,而不是括号中)。我已经尝试在目标的构建设置中将“定义模块”设置为“是”,但它仍然不起作用。我还在构建设置中验证了它具有正确的桥接头文件路径。关于如何设置测试的任何想法?我在 Xcode 6.3.2 上。如果您需要任何其他信息,请告诉我。PickleTestsimport PicklePickleTests.swift



Pickle

Open the image in a new tab to see it larger.

在新选项卡中打开图像以查看更大的图像。

回答by dcgoss

Many thanks to @matt for helping me with this one!

非常感谢@matt 帮助我解决这个问题!

Right click on the images and open them in a new tab to see them larger.

右键单击图像并在新选项卡中打开它们以查看更大的图像。

As discussed on https://github.com/CocoaPods/CocoaPods/issues/2695the issue seemed to be lying with having Cocoapods as part of the project. The answer near the bottom of the link that solves the issue involves clicking on your Application settings, and then clicking on your Project Info(not any of your targets' settings). There you will see your Configurationssettings: enter image description here

正如在https://github.com/CocoaPods/CocoaPods/issues/2695 上讨论的那样,问题似乎在于将 Cocoapods 作为项目的一部分。解决该问题的链接底部附近的答案涉及单击您的应用程序设置,然后单击您的项目Info(不是您的任何目标设置)。在那里你会看到你的Configurations设置:在此处输入图片说明

You will also notice that there are two targets in the config settings, and that your Testing Target config settings will be set to none while your Main Target will be linked to the Cocoapods. The fix lies in changing that Nonevalue next to the Testing Target to the same thing that the Main Target has: enter image description here

您还会注意到配置设置中有两个目标,并且您的测试目标配置设置将设置为无,而您的主目标将链接到 Cocoapods。修复在于None将测试目标旁边的值更改为与主目标相同的值:在此处输入图片说明

So now they are both linked to the Cocoapods. Making sure your main target has Defines moduleset to Yesin its Build Settings, build your project and the error in the testing files should go away.

所以现在它们都与 Cocoapods 相关联。确保您的主要目标已Defines module设置为Yesin its Build Settings,构建您的项目,并且测试文件中的错误应该消失。

Also, after I did this fix I encountered another error where the linker was throwing an error complaining about missing a library in my testing target. This missing library was another dependency I had in my project, and I solved this error by making sure all of my dependencies that were linked in my Main Target's Link Binary with Librariesin its Build Phasessettings were copied over to the Testing Target's Link Binary with Librariesin its Build Phasessettings.

另外,在我完成这个修复之后,我遇到了另一个错误,链接器抛出一个错误,抱怨我的测试目标中缺少一​​个库。这种缺少库是另一个依赖我在我的项目,我通过确保所有我的依赖有联系,在我的主要目标的解决了这个错误Link Binary with Libraries在它的Build Phases设置被复制到检查对象的Link Binary with Libraries在其Build Phases设置。

回答by isuru

Import Module on top of your test class

在测试类之上导入模块

@testable import myModuleName

You can find-out your module name on Target->Build Settings-> Product Module Name

您可以在上找到您的模块名称 Target->Build Settings-> Product Module Name

Then you can use any of class which belongs to that module. No need to import classes one by one.

然后您可以使用属于该模块的任何类。无需一一导入类。

回答by Tomá? Linhart

You don't have to do this. You can use link_within your Podfile. In your case you would do.

你不必这样做。您可以使用link_with在Podfile。在你的情况下,你会这样做。

link_with 'Pickle', 'PickleTests'

And then pod update

进而 pod update

回答by Lance Kind

FWIW, on Xcode 7, import with the annotation: @testable. Example:

FWIW,在 Xcode 7 上,使用注释导入:@testable。例子:

import Pickle
@ImportTests

Then you'll be able to see the classes in code completion and compiling. source: https://www.natashatherobot.com/swift-2-xcode-7-unit-testing-access/

然后您将能够在代码完成和编译中看到这些类。来源:https: //www.natashatherobot.com/swift-2-xcode-7-unit-testing-access/