未在UnitTest中找到模块“MyApp”Swift
问题描述:
我试图在传统的Objc代码中测试一些Swift类(@objc类)。我在我的测试类中导入了“UnitTests-Swift.h”。未在UnitTest中找到模块“MyApp”Swift
然后我得到这个错误:
Module "MyApp" not found in the autogenerated "UnitTests-Swift.h"
这是“单元测试,Swift.h”
typedef int swift_int3 __attribute__((__ext_vector_type__(3)));
typedef int swift_int4 __attribute__((__ext_vector_type__(4)));
#if defined(__has_feature) && __has_feature(modules)
@import XCTest;
@import ObjectiveC;
@import MyApp;
@import CoreData;
#endif
我清理项目的顶部内什么,检查了所有相关的标志("No such module" when using @testable in Xcode Unit tests,Can't use Swift classes inside Objective-C),删除派生数据等..不知道发生了什么,但我很确定@import MyApp不应该在那里..
C任何人都可以帮助我?
答
因为要测试的Swift类是MyApp的一部分,所以应该在测试类中导入“MyApp-Swift.h”而不是“UnitTests-Swift.h”。
答
您可以添加一个Swift单元测试(只需创建一个新的单元文件并将其扩展名更改为.swift
)。
从这个单元测试文件你可以使用你的Swift类。
而且您还可以使用测试模块桥接头从您的Objective-C单元测试(和其他方式)导入该文件。
,这将是你的雨燕单元测试文件的默认实例:
import XCTest
@testable import MyApp
class MyAppSwiftTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
答
刚刚得到在我的项目这一问题,花费在调查了一整天后,我终于解决了。 基本上这是因为你有ObjC和Swift之间的循环依赖。所以,在我的情况是:
- 斯威夫特协议与主目标
@obj
属性; - 继承此协议的UnitTest目标中的Swift类;
- 进口
UnitTestTarget-Swift.h
任何Objective-C类的单元测试的目标
所以相当简单的组合导致这个错误。为了解决这个问题,你想要么:
- 只需确保您的单元测试雨燕类目标是
private
,所以不会去UnitTestTarget-Swift.h
或
- 做不要将您的原始Swift协议标记为
@objc
,这将允许您从所有ObjectiveC测试类访问您的SwiftClass,但这些类不会对Swift协议有任何了解。
我也有这个问题。 – Uncommon
通过请求,我已经在Swift错误数据库中提交了这个:https://bugs.swift。org/browse/SR-3381 – Uncommon
这是可以在Xcode 7中复制的东西,还是需要使用旧版本制作的obj-c项目?你能否提供从头开始重现的步骤? – charmingToad