Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
Ios 为什么在我使用时单元测试覆盖率数据会发生变化??(空合并运算符)?_Ios_Swift_Xcode_Unit Testing_Code Coverage - Fatal编程技术网

Ios 为什么在我使用时单元测试覆盖率数据会发生变化??(空合并运算符)?

Ios 为什么在我使用时单元测试覆盖率数据会发生变化??(空合并运算符)?,ios,swift,xcode,unit-testing,code-coverage,Ios,Swift,Xcode,Unit Testing,Code Coverage,我已经创建了一个雇员模型,其中id和name是可选属性,description方法将返回一个插值字符串 代码如下: class Employee: NSObject { var id: String? var name: String? init(id: String, name: String) { self.id = id self.name = name } func description() -> Str

我已经创建了一个雇员模型,其中id和name是可选属性,description方法将返回一个插值字符串

代码如下:

class Employee: NSObject {
    var id: String?
    var name: String?

    init(id: String, name: String) {
        self.id = id
        self.name = name
    }

    func description() -> String {
        return "Employee ID: \(id ?? "") and Employee Name: \(name ?? "")."
        //return "Employee ID: \(id != nil ? id! : "") and Employee Name: \(name ?? "")."
    }
}
class EmployeeTests: 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 testEmployee() {
        let employee = Employee(id: "1", name: "Yash Vyas")
        XCTAssertEqual(employee.id, "1")
        XCTAssertEqual(employee.name, "Yash Vyas")
        XCTAssertEqual(employee.description(), "Employee ID: 1 and Employee Name: Yash Vyas.")
    }
}
我已经为单元测试这个员工模型编写了测试用例

代码如下:

class Employee: NSObject {
    var id: String?
    var name: String?

    init(id: String, name: String) {
        self.id = id
        self.name = name
    }

    func description() -> String {
        return "Employee ID: \(id ?? "") and Employee Name: \(name ?? "")."
        //return "Employee ID: \(id != nil ? id! : "") and Employee Name: \(name ?? "")."
    }
}
class EmployeeTests: 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 testEmployee() {
        let employee = Employee(id: "1", name: "Yash Vyas")
        XCTAssertEqual(employee.id, "1")
        XCTAssertEqual(employee.name, "Yash Vyas")
        XCTAssertEqual(employee.description(), "Employee ID: 1 and Employee Name: Yash Vyas.")
    }
}

当我使用三元运算符而不是??(description方法中的注释行)用于在从description()方法返回插值字符串时检查nil的可选属性,以及在使用时为什么会减少??(空合并)运算符。

添加另一个这样的情况可以解决问题:

let another = Employee(id: "1", name: "Yash Vyas")
another.id = nil
XCTAssertEqual(another.description(), "Employee ID:  and Employee Name: Yash Vyas.")
虽然在我看来,这可能是llvm或Xcode端的一个bug