【问题标题】:Module "MyApp" not found in UnitTest-Swift在 UnitTest-Swift 中找不到模块“MyApp”
【发布时间】:2016-02-14 19:12:00
【问题描述】:

我试图在我的旧 Objc 代码中测试一些 Swift 类(@objc 类)。我在我的测试类中导入“UnitTests-Swift.h”。

然后我得到这个错误:

在自动生成的“UnitTests-Swift.h”中找不到模块“MyApp”

这是“UnitTests-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 testsCan't use Swift classes inside Objective-C),删除了派生数据等等。不知道发生了什么,但我很确定 @import MyApp 不应该在那里。 .

谁能帮我解决这个问题?

【问题讨论】:

  • 我也遇到了这个问题。
  • 应要求,我已将其归档在 Swift 错误数据库中:bugs.swift.org/browse/SR-3381
  • 这是可以在 Xcode 7 中重现的东西,还是需要使用旧版本制作的 obj-c 项目?你能提供从头开始重现的步骤吗?
  • swift 和 Objective-C 类是否在同一个目标中?

标签: ios xcode swift2 swift3 xcode7


【解决方案1】:

刚刚在我的项目中遇到了这个问题,经过一整天的调查,我终于解决了它。 基本上这是因为您在 ObjC 和 Swift 之间存在循环依赖关系。所以在我的情况下是:

  • 主目标中带有@obj 属性的Swift 协议;
  • 继承此协议的 UnitTest 目标中的 Swift 类;
  • 在 UnitTest 目标的任何 Objective-C 类中导入 UnitTestTarget-Swift.h

如此简单的组合导致了这个错误。为了解决这个问题,您需要:

  • 只需确保您在 UnitTest 目标中的 Swift 类是 private,这样它就不会到达 UnitTestTarget-Swift.h

  • 不要将您的原始 Swift 协议标记为 @objc,这将允许您从所有 ObjectiveC 测试类访问您的 SwiftClass,但这些类不会对 Swift 协议有任何了解。

【讨论】:

    【解决方案2】:

    因为要测试的 Swift 类是 MyApp 的一部分,所以您应该在测试类中导入“MyApp-Swift.h”而不是“UnitTests-Swift.h”。

    【讨论】:

      【解决方案3】:

      您可以添加一个 Swift 单元测试(只需创建一个新的单元文件并将扩展名更改为 .swift)。

      从该单元测试文件中,您可以使用您的 Swift 类。

      您还可以使用测试模块桥接头从您的 Objective-C 单元测试(以及其他方式)导入该文件。

      这将是您的 Swift 单元测试文件的默认示例:

      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.
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        这个解决方案帮助了我:

        1. 打开测试目标构建设置
        2. 搜索HEADER_SEARCH_PATHS
        3. 在名为“Header Search Paths”的行中设置值$CONFIGURATION_TEMP_DIR/myProjectName.build/DerivedSources
        4. 再次清理并 Cmd+U

        希望对你有帮助!

        非常感谢article

        【讨论】:

          猜你喜欢
          • 2021-08-09
          • 2023-03-22
          • 1970-01-01
          • 2013-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多