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

≡放荡痞女 提交于 2019-12-12 03:44:44

问题


I have tried adding a testing bundle to my Project which seems to have succeeded.

However when I try to create instances of the Classes in my main project- I am unable to see them.

The Project seems to build fine - but I can' instantiate any of the test objects

Any ideas how to access them

Example Class to Test:

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

}

回答1:


I managed to get it working by adding Testable to the top of the class( This appears to be OSX specific issue)

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

}

Also be sure to clean your project after adding it an it seems to work.



来源:https://stackoverflow.com/questions/38620434/unable-to-access-main-target-methods-when-adding-tests-to-osx-project-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!