2015-11-13 39 views
13

我試圖在傳統的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任何人都可以幫助我?

+0

我也有這個問題。 – Uncommon

+0

通過請求,我已經在Swift錯誤數據庫中提交了這個:https://bugs.swift。org/browse/SR-3381 – Uncommon

+1

這是可以在Xcode 7中複製的東西,還是需要使用舊版本製作的obj-c項目?你能否提供從頭開始重現的步驟? – charmingToad

回答

0

因爲要測試的Swift類是MyApp的一部分,所以應該在測試類中導入「MyApp-Swift.h」而不是「UnitTests-Swift.h」。

-1

您可以添加一個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. 
    } 
    } 
} 
1

剛剛得到在我的項目這一問題,花費在調查了一整天后,我終於解決了。 基本上這是因爲你有ObjC和Swift之間的循環依賴。所以,在我的情況是:

  • 斯威夫特協議與主目標@obj屬性;
  • 繼承此協議的UnitTest目標中的Swift類;
  • 進口UnitTestTarget-Swift.h任何Objective-C類的單元測試的目標

所以相當簡單的組合導致這個錯誤。爲了解決這個問題,你想要麼:

  • 只需確保您的單元測試雨燕類目標是private,所以不會去UnitTestTarget-Swift.h

  • 做不要將您的原始Swift協議標記爲@objc,這將允許您從所有ObjectiveC測試類訪問您的SwiftClass,但這些類不會對Swift協議有任何瞭解。