向 Swift 中的 OSX 项目添加测试时无法访问主要目标方法

Unable to access Main Target Methods when Adding Tests to OSX Project in Swift

我尝试将测试包添加到我的项目中,这似乎成功了。

然而,当我尝试在我的主项目中创建 Classes 的实例时,我无法看到它们。

项目似乎构建良好 - 但我无法实例化任何测试对象

关于如何访问它们的任何想法

要测试的示例 Class:

class EmailHelper: NSObject {
    func generateEmailBody (greeting: String, bodyContent: String) -> String {
        //Content goes in here
return message
    }
}

import XCTest

class MyProject_DesktopTests: 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() {
    // The Test would go in here but I can't seem to resolve EmailHelper class- it generates a lint error
        // 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.measureBlock {
            // Put the code you want to measure the time of here.
        }
    }

}

我设法通过将 Testable 添加到 class(这似乎是 OSX 特定问题)

的顶部使其工作
import XCTest
@testable import MyProjectName // <--- This was the missing bit.... :)
class MyProject_DesktopTests: 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() {
    // The Test would go in here but I can't seem to resolve EmailHelper class- it generates a lint error
        // 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.measureBlock {
            // Put the code you want to measure the time of here.
        }
    }

}

另外请务必在添加项目后清理您的项目,它似乎可以正常工作。