2015-11-02 80 views
0

如何在快速單元測試中測試2個字符串是否相等?我試過==操作符,但它不承認它:在快速單元測試中比較字符串

import XCTest 
@testable import MyProject 

class MyProject: 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. 
    XCTAssertNil(nil, "This test works") 
} 

func toJSONTest() { 
    let currentLocation = deviceLocation(timestamp: "2015-11-02 16:32:15 +0000",latitude: "40.2736577695212",longitude: "-111.715408331498") 
    var MyProjectStatuses = [MyProjectStatus(id: "", currentLocation: currentLocation)] 
    let json = "" 
    XCTAssertTrue(json == "") 
} 

func testPerformanceExample() { 
    // This is an example of a performance test case. 
    self.measureBlock { 
     // Put the code you want to measure the time of here. 
    } 
} 

} 

而且從MyProject.swift正在測試的實際方法:

func toJSON()->String{ 
    var json = "" 
    json = "{\"myproject_status\":" 
    json = json + "{\"id\":\"" + self.Id + "\"" 
    return json 
} 

這一部分:

XCTAssertTrue(json == "")

預測:

Operator is not a known binary operator

+1

請顯示_whole_單元測試文件。 – matt

回答

3

問題是toJSONTest不是測試。將名稱更改爲testToJSON

這工作我的機器罰款:

func testToJSON() { 
    let json = "" 
    XCTAssertTrue(json == "") 
} 

的測試運行,並通過。但是,我可能會這樣寫:

func testToJSON() { 
    let json = "" 
    XCTAssertEqual(json, "", "They are not equal") 
} 
+0

另外,觀察你的測試,如果它是一個測試,無處叫'func toJSON',所以它實際上並沒有測試任何東西。 – matt

+0

然後我改變它來弄清楚如何比較字符串。它仍然拋出相同的錯誤,名稱testToJSON() – JBaczuk

+0

在我的機器上正常工作。 – matt