Objective C使用Swift代码测试类 [英] Objective C test class using Swift code
问题描述
我在 MyProjectName
文件夹中有一个swift协议 IModelIdProvider
。
我在 MyProjectNameTests
文件夹中创建了一个名为 RemoteModelFactoryTest
的objective-c单元测试类。
要进行一些测试, RemoteModelFactoryTest
必须实现 IModelIdProvider
协议。
I have a swift protocol IModelIdProvider
in the MyProjectName
folder.
I have created an objective-c unit test class named RemoteModelFactoryTest
in the MyProjectNameTests
folder.
To do some tests, RemoteModelFactoryTest
must implement the IModelIdProvider
protocol.
当我 #importMyProjectName-Swift.h
在我的objective-c类中使用我的swift协议时,我得到找不到文件
。
When I #import "MyProjectName-Swift.h"
to use my swift protocol in my objective-c class, I get a file not found
.
如果我将 #import
指令更改为 #importMyProjectNameTests-Swift.h
,找到标题但不是我的协议(它在 MyProjectName
项目中定义,而不是在 MyProjectNameTests
项目中。
If I change the #import
instruction to #import "MyProjectNameTests-Swift.h"
, the header is found but not my protocol (it's defined in the MyProjectName
project, not in the MyProjectNameTests
project).
在中有什么特别的事吗? *测试
使用Swift代码的项目?
Is there something special to do in the *Tests
projects to use Swift code ?
推荐答案
我知道它的额外hacky和一切,但是什么我做了,它的工作原理是从生成的Project-Swift.h复制公共标题并粘贴 .m 测试文件所需的内容。
I know its extra hacky and everything, but what I did and it worked was to copy public headers from generated "Project-Swift.h" and paste whats needed to .m file of test.
所以我复制了这样的东西:
So I copied something like this:
SWIFT_CLASS("_TtC10Inventorum11ImageEntity")
@interface ImageEntity : NSObject <FICEntity>
@property (nonatomic, copy) NSString * imageID;
@property (nonatomic) NSURL * retinaImageURL;
@property (nonatomic) NSURL * nonRetinaimageURL;
@property (nonatomic, readonly) NSURL * imageURL;
@property (nonatomic, readonly, copy) NSString * UUID;
@property (nonatomic, readonly, copy) NSString * sourceImageUUID;
@property (nonatomic) RACSignal * rac_signalForImage;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
- (NSURL *)sourceImageURLWithFormatName:(NSString *)formatName;
- (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
- (void)objc_setType:(NSString *)type;
@end
除此之外,我还复制了 SWIFT_CLASS 等我的测试正在构建并通过测试Swift代码。
And on top of this I have also copied all the macros for SWIFT_CLASS etc. My tests are building and passing testing Swift code.
BTW。别忘了在Swift中对测试的类和方法进行公开。
BTW. Dont forget to put public on tested classes and methods in Swift.
这篇关于Objective C使用Swift代码测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!