xcode 在 Swift 项目中问题测试和使用 Cocoapods
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25495225/
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
Issue testing and using Cocoapods in a Swift project
提问by JJSaccolo
I'm writing an app in Swift, using XCode 6 Beta-6. I'm using Cocoapods and I'm creating some unit tests.
我正在 Swift 中编写一个应用程序,使用 XCode 6 Beta-6。我正在使用 Cocoapods 并且正在创建一些单元测试。
The issue is this one: apparently is not possible to have a project that contains:
问题是这样的:显然不可能有一个包含以下内容的项目:
1) Project written in Swift
1) 用 Swift 编写的项目
2) Some pods installed using cocoapods
2)一些使用cocoapods安装的pods
3) A Objective-C bridge header file that imports some pods
3) 一个导入一些 pod 的 Objective-C 桥头文件
4) Unit tests
4) 单元测试
This sounds weird, but follow my steps: after running pod install
, create the Objective-C bridge header and import one pod: everything works.
Now write some tests: in order to test your own classes, you have to import the module called "as your project" (or better, "as the main target"): in my "MyAwesomeApp" project I have to write import MyAwesomeApp
in my tests files.
这听起来很奇怪,但请按照我的步骤操作:运行后pod install
,创建 Objective-C 桥头并导入一个 pod:一切正常。现在编写一些测试:为了测试你自己的类,你必须导入名为“作为你的项目”(或者更好,“作为主要目标”)的模块:在我的“MyAwesomeApp”项目中,我必须import MyAwesomeApp
在我的测试中编写文件。
Unfortunately, at this step XCode won't compile:
in my import MyAwesomeApp
line with the error "Failed to import bridging header '/path/to/MyAwesomeApp/MyAwesomeApp/MyAwesomeApp-Bridging-Header.h";
and the error "xxx.h file not found" appears in the Bridging-Header file, excluding the possibility to import a pod.
不幸的是,在这一步 XCode 不会编译:在我的import MyAwesomeApp
行中,错误是“无法导入桥接头‘/path/to/MyAwesomeApp/MyAwesomeApp/MyAwesomeApp-Bridging-Header.h”;
并且在 Bridging-Header 文件中出现错误“xxx.h file not found”,排除了导入 pod 的可能性。
Also, if I don't import the pods in the Obj-c bridge file, the project will compile fine.
另外,如果我不在 Obj-c 桥文件中导入 pod,该项目将可以正常编译。
It looks that there is a conflict importing both the Objective-C Bridge Header (with Objective-C files taken from a different sub-project in the workspace) and the "main module" used for testing.
看起来在导入 Objective-C 桥头文件(从工作区中的不同子项目中获取的 Objective-C 文件)和用于测试的“主模块”都存在冲突。
Do you know if there is a solution? What am I missing? Thanks
不知道有没有解决办法?我错过了什么?谢谢
NOTE: As a workaround, I could import the pods in the Objective-C Bridge Header, and, instead of include the main module in my tests, add all the classes that I want to test in my "test" target. This will work, but it's not the most clean solution (I think)
注意:作为一种变通方法,我可以在 Objective-C Bridge Header 中导入 pod,而不是在我的测试中包含主模块,而是在我的“测试”目标中添加我想要测试的所有类。这会起作用,但它不是最干净的解决方案(我认为)
采纳答案by sergio
If you take a look at your main target Build Settings, you will see that there are a bunch of directories listed for the "Header Search Paths" settings.
如果您查看主要目标构建设置,您将看到为“标题搜索路径”设置列出了一堆目录。
You either need to copy those values under the test target, or you can try and modify your Podfile to include both your main and test targets and re-run install
:
您需要在测试目标下复制这些值,或者您可以尝试修改 Podfile 以包含主目标和测试目标并重新运行install
:
platform :ios, '7.0'
link_with 'mainapp', 'mainappTests'
...
Also take care of any other header paths that could be needed and are not related to CocoaPods.
还要注意可能需要且与 CocoaPods 无关的任何其他标头路径。
And don't forget that your classes shall have public methods wherever you want to unit-test them.
并且不要忘记你的类应该有公共方法,只要你想对它们进行单元测试。
Hope this helps.
希望这可以帮助。
回答by Alberto Salas
Maybe you have configured the "Objective-C Bridging Header" setting at Project level, so the "Test" target inherits that value and maybe this "Test" target is not linked with Cocoapods.
也许您已经在项目级别配置了“Objective-C Bridging Header”设置,因此“Test”目标继承了该值,并且这个“Test”目标可能未与 Cocoapods 链接。
Use link_with
as @sergio suggests or set the "Pods*.debug/release" configuration for the "Test" target at "Project->Info->Configuration".
link_with
按照@sergio 的建议使用或在“Project->Info->Configuration”中为“Test”目标设置“Pods*.debug/release”配置。
回答by Kevin Goedecke
In addition to the link_with
command in my Podfile i had to import my main project module in the test file. This way classes and methods don't have to public.
除了link_with
Podfile 中的命令之外,我还必须在测试文件中导入我的主项目模块。这样类和方法不必公开。
Note the special @testable annotation
注意特殊 @testable annotation
@testable import my_tutorial_app
@testable import my_tutorial_app
Also my main project name had non-alphanumerical characters in it, I had to replace them with underscores _
另外我的主要项目名称中包含非字母数字字符,我不得不用下划线替换它们 _