简体   繁体   中英

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. They require access to Objective-C classes which only exist in my test target .

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. However, I can't then use that to expose Objective-C classes in my test target.

How do I access the Objective-C classes that only exist in my test target, from a Swift test class?

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.

Call it "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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