Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在Swift单元测试中比较字符串_String_Swift_Unit Testing - Fatal编程技术网

String 在Swift单元测试中比较字符串

String 在Swift单元测试中比较字符串,string,swift,unit-testing,String,Swift,Unit Testing,如何在Swift单元测试中测试两个字符串是否相等?我尝试了==操作符,但它无法识别: 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

如何在Swift单元测试中测试两个字符串是否相等?我尝试了
==
操作符,但它无法识别:

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 
}
本部分:

xctasertrue(json==”)

抛出:


运算符不是已知的二进制运算符

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

这在我的机器上运行良好:

func testToJSON() {
    let json = ""
    XCTAssertTrue(json == "")
}
测试运行,并通过。然而,我可能会这样写:

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

尽管这个问题是关于如何在Swift单元测试中比较两个字符串的,但问题中隐含的是如何比较两个JSON字符串。我只是想指出,当比较两个JSON字符串时,正确的做法是将JSON字符串解析为带有类的基础对象,然后比较生成的基础对象。这种方法解决了两个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)
    }
}

请显示整个单元测试文件。另外,请注意您的测试,如果它是一个测试,则没有任何地方调用
func-toJSON
,因此它实际上没有测试任何内容。然后我更改了它以了解如何比较字符串。它仍然抛出相同的错误,名称为testToJSON(),在我的机器上运行良好。我使用的是XCode版本7.1 beta 3(7B85),如果这很重要,为什么要使用beta?7.1不久前进入决赛。我认为这不重要,但这样做是愚蠢的。