2016-07-27 20 views
0

Ich habe versucht, meinem Projekt ein Testpaket hinzuzufügen, das anscheinend erfolgreich war.Zugriff auf Hauptzielmethoden beim Hinzufügen von Tests zum OSX-Projekt in Swift nicht möglich

Wenn ich jedoch versuche, Instanzen der Klassen in meinem Hauptprojekt zu erstellen, kann ich sie nicht sehen.

Das Projekt scheint in Ordnung zu bauen - aber ich kann‘instanziiert eine der Testobjekte

Irgendwelche Ideen, wie man sie Test-

Beispiel Klasse zuzugreifen:

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

} 
+0

Wo instanziieren Sie Ihre Klassen? Bitte fügen Sie diesen Code hier ein. – Santosh

+0

Die Antworten auf [diese Seite] (http://stackoverflow.com/questions/29192778/how-do-i-access-my-app-code-from-my-tests-in-a-swift-project/29193159 # 29193159) kann helfen. –

+0

Ich habe eine Beispielklasse hinzugefügt, um die obige Frage zu testen. Ich habe versucht, @testable Import MyProjectName vor der Klasse hinzuzufügen - aber es scheint nicht in der Lage zu sein, mein Hauptziel zu erkennen, so dass es immer noch nicht funktioniert. –

Antwort

0

ich es geschafft, Lassen Sie es funktionieren, indem Sie Testable an der Spitze der Klasse hinzufügen (Dies scheint ein OSX-spezifisches Problem zu sein)

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

} 

Achten Sie auch darauf, Ihr Projekt nach dem Hinzufügen zu säubern und es scheint zu funktionieren.