在Swift项目中进行测试和使用Cocoapods [英] Issue testing and using Cocoapods in a Swift project

查看:76
本文介绍了在Swift项目中进行测试和使用Cocoapods的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XCode 6 Beta-6在Swift中编写一个应用。
我正在使用Cocoapods,并且正在创建一些单元测试。

I'm writing an app in Swift, using XCode 6 Beta-6. I'm using Cocoapods and I'm creating some unit tests.

问题是这样的:显然不可能有一个包含以下内容的项目:

The issue is this one: apparently is not possible to have a project that contains:

1)用Swift编写的项目

1) Project written in Swift

2)使用cocoapods安装了一些吊舱

2) Some pods installed using cocoapods

3)导入一些Pod的Objective-C桥头文件

3) A Objective-C bridge header file that imports some pods

4)单元测试

这听起来很奇怪,但是请按照我的步骤进行:运行 pod install 之后,创建Objective-C桥头并导入一个pod:一切正常。
现在编写一些测试:为了测试您自己的类,您必须导入名为作为您的项目(或者更好的是作为主要目标)的模块:在我的 MyAwesomeApp项目中,在我的测试文件中写入 import MyAwesomeApp

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.

不幸的是,在此步骤XCode无法编译:
在我的 import MyAwesomeApp 行中,错误为未能导入桥接头'/path/to/MyAwesomeApp/MyAwesomeApp/MyAwesomeApp-Bridging-Header.h;

和错误 xxx.h文件未找到出现在Bridging-Header文件中,不包括导入Pod的可能性。

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.

此外,如果我不将吊舱导入到Obj-c桥文件中,则项目会编译良好。

Also, if I don't import the pods in the Obj-c bridge file, the project will compile fine.

在导入Objective-C桥头(从工作空间中的另一个子项目获取的Objective-C文件)和用于测试的主模块之间似乎存在冲突。

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.

您知道是否有解决方案吗?我想念什么?
谢谢

Do you know if there is a solution? What am I missing? Thanks

注意:作为一种解决方法,我可以将Pod导入到Objective-C桥头中,而不是在测试中包含主模块,将所有要测试的类添加到测试目标中。
这将起作用,但它不是最干净的解决方案(我认为)

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)

推荐答案

如果您看看您的主要目标构建设置,您将看到 Header Search Paths设置中列出了一堆目录。

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.

您要么需要在测试目标下复制这些值,要么可以尝试修改Podfile使其同时包含主要目标和测试目标,然后重新运行安装

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:

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.

并且不要忘记,您的类在要进行单元测试的任何地方都应该具有公共方法。

And don't forget that your classes shall have public methods wherever you want to unit-test them.

希望这会有所帮助。

这篇关于在Swift项目中进行测试和使用Cocoapods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆