Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Strings in Swift unit test

How do you test whether two Strings are equal in a Swift unit test? I've tried the == operator but it doesn't recognize it:

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

}

And the actual method being tested from MyProject.swift:

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

This part:

XCTAssertTrue(json == "")

Throws:

Operator is not a known binary operator

like image 780
JBaczuk Avatar asked Oct 28 '25 15:10

JBaczuk


1 Answers

The problem is that toJSONTest is not a test. Change the name to testToJSON.

This works fine on my machine:

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

The test runs, and passes. However, I would probably write it like this:

func testToJSON() {
    let json = ""
    XCTAssertEqual(json, "", "They are not equal")
}
like image 150
matt Avatar answered Oct 30 '25 07:10

matt