简体   繁体   English

如何从Swift单元测试中访问测试目标中存在的Objective-C代码?

[英]How do I access Objective-C code that exists in the test target, from a Swift unit test?

I'm trying to unit test some Swift classes I've written. 我正在尝试对我编写的一些Swift类进行单元测试。 They require access to Objective-C classes which only exist in my test target . 它们需要访问仅存在于我的测试目标中的 Objective-C类。

I can access the Objective-C classes belonging to my app target by specifying the app's bridging header in the test targets "Objective-C Bridging Header" build setting. 我可以通过在测试目标“Objective-C Bridging Header”构建设置中指定app的桥接头来访问属于我的app目标的Objective-C类。 However, I can't then use that to expose Objective-C classes in my test target. 但是,我不能使用它来在我的测试目标中公开Objective-C类。

How do I access the Objective-C classes that only exist in my test target, from a Swift test class? 如何从Swift测试类中访问仅存在于我的测试目标中的Objective-C类?

I added a second bridging header file, this one belongs within my test target. 我添加了第二个桥接头文件,这个属于我的测试目标。 Within this second header, I import the one from my app, along with the Objective-C code I need inside my tests. 在第二个标题中,我从我的应用程序中导入了一个,以及我在测试中需要的Objective-C代码。

Call it "MyAppTests-BridgingHeader.h": 称之为“MyAppTests-BridgingHeader.h”:

#import "MyApp-Bridging-Header.h"
#import "MagicalRecord.h"
#import "MagicalRecord+Setup.h"

I then set this as the bridging header in the test target build settings: 然后我将其设置为测试目标构建设置中的桥接头:

在此输入图像描述

My tests now have access to all the Objective-C classes exposed to Swift in my main bridging header, as well as all extra Objective-C classes exposed only within the tests. 我的测试现在可以访问我的主桥接头中暴露给Swift的所有Objective-C类,以及仅在测试中公开的所有额外Objective-C类。

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

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