在swift单元测试中比较字符串

时间:2015-11-02 17:31:07

标签: string swift unit-testing

如何在快速单元测试中测试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

2 个答案:

答案 0 :(得分:5)

问题是toJSONTest不是测试。将名称更改为testToJSON

这在我的机器上工作正常:

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

测试运行并通过。但是,我可能会这样写:

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

答案 1 :(得分:0)

尽管此问题明确地涉及如何在Swift单元测试中比较两个String,但问题中隐含的是如何比较两个JSON String。我只想指出,比较两个JSON字符串时,正确的做法是使用JSONSerialization类将JSON字符串解析为Foundation对象,然后比较生成的Foundation对象。此方法解决了两个JSON字符串的格式或字段顺序不同的问题。因此,例如,"{\"a\":1,\"b\":2}""{\"b\":2,\"a\":1}"被视为相等是很重要的,因为它们在逻辑上是相等的。

这是我整理的Swift函数,有助于进行比较:

class JSONAssert {

    class func assertEquals(expected: String, actual: String) {
    
        let expectedData = Data(expected.utf8)
        let actualData = Data(actual.utf8)
    
        let expectedObject: Any
        let actualObject: Any
    
        do {
            expectedObject = try JSONSerialization.jsonObject(with: expectedData, options: [])
        } catch {
            XCTFail("Failed constructing a Foundation object from `expected` (i.e. \(expected)): \(error)")
            return
        }
    
        do {
            actualObject = try JSONSerialization.jsonObject(with: actualData, options: [])
        } catch {
            XCTFail("Failed constructing a Foundation object from `actual` (i.e. \(actual)): \(error)")
            return
        }
    
        guard let expectedDictionary = expectedObject as? NSDictionary else {
            XCTFail("Failed casting expected object (i.e. \(expectedObject)) to an NSDictionary")
            return
        }
    
        guard let actualDictionary = actualObject as? NSDictionary else {
            XCTFail("Failed casting actual object (i.e. \(actualObject)) to an NSDictionary")
            return
        }
    
        XCTAssertEqual(expectedDictionary, actualDictionary)
    }
}