2017-04-12 60 views
2

我正在爲我的(macOS)Xcode項目嘗試編寫單元測試(不是 UI測試)。我創建了一個新的單元測試目標,爲我創建一個空白單元測試文件。Swift 3單元測試 - 使用導入時鏈接器失敗@testable

這符合和鏈接很好,但沒有測試。

只要我添加一行@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. 
     } 
    } 

} 

回答

4

如果您的CocoaPods框架未包含在測試目標中。它會拋出這個錯誤。

我確保運行pod安裝,但仍然失敗。

因此,'pod deintegrate Yourproject.xcodeproj'並重新安裝(pod install),清除問題。

2

,因爲項目失敗鏈接在一起的導入失敗。我下載了它,我收到以下錯誤試圖運行它:

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

嘗試清理您的build文件夾,或項目下載到一個新的文件夾,並解決這個問題... ...之後,你就可以能夠編譯和@可測試導入Pilot。

+1

這指出我在正確的方向。結果我的CocoaPods框架沒有包含在測試目標中(不是CocoaPods 1.0.0之前的問題),所以修復它解決了我的鏈接問題。謝謝! – Rohan

0

將主機應用程序設置爲您的項目目標。 另外,請選中允許測試主機應用程序API已打開。

enter image description here