【问题标题】:Swift 3 Unit Testing - Linker fails when using import @testableSwift 3 单元测试 - 使用 import @testable 时链接器失败
【发布时间】:2017-04-12 01:54:45
【问题描述】:

我目前正在尝试为我的 (macOS) Xcode 项目编写单元测试(NOT UI 测试)。我创建了一个新的单元测试目标,它为我创建了一个空白单元测试文件。

这符合并链接正常,但没有测试。

只要我添加行@testable import Pilot,其中Pilot 是我的应用程序目标的名称,我尝试编译和运行,它就会失败并显示以下消息:

Linker command failed with exit code 1 (use -v to see invocation)

我已经尝试了所有能找到的方法,但似乎没有任何效果。我在这里阅读的其他帖子在 UI 测试中处理了这个问题,但那是因为您不能在 UI 测试中使用 @testable。您应该能够在单元测试中使用它,但我无法弄清楚为什么这不起作用。

有人有什么见解吗?

如果有帮助,我的项目位于:https://github.com/RohanNagar/pilot-osx

这是完整的PilotTests.swift 文件:

import XCTest

@testable import Pilot

class PilotTests: 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.
        }
    }

}

【问题讨论】:

    标签: swift xcode unit-testing cocoa linker


    【解决方案1】:

    如果您的 CocoaPods 框架未包含在测试目标中。它会抛出这个错误。

    我确保运行 pod 安装,但仍然失败。

    所以“pod deintegrate Yourproject.xcodeproj”并重新安装(pod install),即可解决问题。

    【讨论】:

      【解决方案2】:

      导入失败,因为项目无法链接在一起。我下载了它,但尝试运行它时出现以下错误:

      Showing Recent Issues
      ld: framework not found Realm for architecture x86_64
      

      尝试清理您的构建文件夹,或将项目下载到新文件夹,并修复此问题...之后您将能够编译和@testable import Pilot。

      【讨论】:

      • 这为我指明了正确的方向。原来我的 CocoaPods 框架都没有包含在测试目标中(在 CocoaPods 1.0.0 之前不是问题),因此修复它解决了我的链接问题。谢谢!
      【解决方案3】:

      将主机应用程序设置为您的项目目标。 此外,检查允许测试主机应用程序 API 是否已打开。

      【讨论】:

        【解决方案4】:
        1. 选择您的测试目标
        2. 选择构建阶段
        3. 在框架搜索路径中,添加此 "${PODS_CONFIGURATION_BUILD_DIR}/"recursive 选项

        【讨论】:

          猜你喜欢
          • 2018-08-31
          • 2015-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 2013-12-25
          • 2010-11-28
          相关资源
          最近更新 更多