简体   繁体   English

如何在XCTest上使用静态库(例如cocoapods库)?

[英]How to use a static library (e.g. cocoapods library) on a XCTest?

I'm working with Core Data and, as I the model gets more complex, I need to make sure that the new changes I introduce don't break my model unexpectedly in other parts. 我正在使用Core Data,因为我的模型变得更复杂,我需要确保我引入的新更改不会在其他部分意外地破坏我的模型。

I can create unit tests and run them every time I change something on my model. 我可以创建单元测试并在每次更改模型上的内容时运行它们。 If something breaks, there might be something wrong with my model or at least I know I have to modify some queries in the main code/tests. 如果出现问题,我的模型可能有问题,或者至少我知道我必须在主代码/测试中修改一些查询。

I'm using MagicalRecord to have access to some convenience methods. 我正在使用MagicalRecord来访问一些方便的方法。 I also use cocoapods for the same reason, convenience. 我也使用cocoapods出于同样的原因,方便。 The problem is that cocoapods creates a static library and links it against my target, but in Xcode, new test targets are not automatically configured to link against the same libraries/frameworks the target in question links against to. 问题是cocoapods创建了一个静态库并将其链接到我的目标,但在Xcode中,新的测试目标不会自动配置为链接到相关目标链接的相同库/框架。

How can I have a XCTest link against a static library? 如何针对静态库创建XCTest链接?

This is not only helpful with MagicalRecord/Core Data, but when you're using an external library it's a good idea to have tests to make sure that updates on the library don't break your App. 这不仅有助于MagicalRecord / Core Data,但是当您使用外部库时,最好进行测试以确保库上的更新不会破坏您的应用程序。

If you're using cocoapods, you can simply use link_with to include your test target, but if you're using a static library not created by cocoapods you can do the following: 如果你正在使用cocoapods,你可以简单地使用link_with来包含你的测试目标,但是如果你使用的是非 cocoapods创建的静态库,你可以执行以下操作:

(I will still use a cocoapods library for the instructions, as that's what I'm working with, but the idea is the same if you're not using a cocoapods library) (我仍然会使用cocoapods库来获取指令,因为我正在使用它,但如果您不使用cocoapods库,那么这个想法是一样的)

Once you have created a new Test Target, click on the project root node in the project navigator and select your test target. 创建新的测试目标后,单击项目导航器中的项目根节点,然后选择测试目标。 Go to Build Settings and search for Header Search Paths . 转到“ Build Settings并搜索“ Header Search Paths Double click on the Header Search Paths item and enter ${SRCROOT}/Pods/Headers and select recursive if you want to import all of your cocoapods libraries headers or enter them individually: ${SRCROOT}/Pods/Headers/MagicalRecord leaving non-recursive selected (although in this case it doesn't really matter). 双击Header Search Paths项并输入${SRCROOT}/Pods/Headers并选择recursive如果要导入所有cocoapods库标题或单独输入: ${SRCROOT}/Pods/Headers/MagicalRecord离开non-recursive选择(虽然在这种情况下它并不重要)。

Now search for Linking and in Other Linker Flags add -ObjC 现在搜索LinkingOther Linker Flags添加-ObjC

Now with your Test Target still selected, go to Build Phases and in Link Binary With Libraries click on the + and add libPods.a or the other libraries individually ( libPods-MagicalRecord.a ) 现在仍然选择了测试目标,转到Build Phases并在Link Binary With Libraries单击+并单独添加libPods.a或其他库( libPods-MagicalRecord.a

You should be able to run a XCTest using the static library. 您应该能够使用静态库运行XCTest。

Optional: I like to import the headers I know I'm going to use in the -Prefix.pch file. 可选:我想导入我知道我将在-Prefix.pch文件中使用的标头。 You can go to your target test group in the Project Navigator. 您可以在Project Navigator中转到目标测试组。 Go to the Supporting Files group and open the -Prefix.pch file. 转到Supporting Files组并打开-Prefix.pch文件。 For MagicalRecord I like to add: 对于MagicalRecord,我想添加:

#define MR_SHORTHAND
#import "CoreData+MagicalRecord.h"

For more information: 欲获得更多信息:

After a lot of fighting, these steps worked for me: 经过大量的战斗,这些步骤对我有用:

1) Project> Info 1)项目>信息

On configurations, set the Test Target to share the same Configuration File as your main project (generated by Cocoapods). 在配置上,将测试目标设置为与主项目共享相同的配置文件(由Cocoapods生成)。

在此输入图像描述

Now, you should start to get some errors because XCUnit framework is missing, but now your external libraries imported with CocoaPod are visible on your test project. 现在,您应该开始出现一些错误,因为缺少XCUnit框架,但现在您的CocoaPod导入的外部库在您的测试项目中可见。

2) On the Test Target>Build Settings look for Header Search Paths, once there add: 2)在测试目标>构建设置上查找标题搜索路径,一旦添加:

$(DEVELOPER_DIR)/Platforms/iPhoneOS.platform/Developer/Library/Frameworks
$(DEVELOPER_DIR)/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks

The Unit Test framework is inside your Xcode App, this headers will make them public to be added later. 单元测试框架位于您的Xcode应用程序中,此标题将使它们公开以便稍后添加。

3) On the Test Target> Build Phases add the SenTestingKit.framework 3)在Test Target> Build Phases上添加SenTestingKit.framework

在此输入图像描述

And it should look like this 它看起来应该是这样的

在此输入图像描述

From there, everything seems to work for me. 从那里,一切似乎对我有用。 Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM