【发布时间】:2018-01-19 14:38:22
【问题描述】:
在我的应用程序中(仅显示相关代码),我有一个带有属性的类 Test
var location: CLLocation
我使用
归档它public func encode(with aCoder: NSCoder) {
aCoder.encode(location, forKey: "location")
}
使用
取消归档required convenience public init?(coder aDecoder: NSCoder) {
let unarchivedLocation = aDecoder.decodeObject(forKey: "location") as! CLLocation
self.init(location: unarchivedLocation)
}
单元测试使用
func test_archiningUnarchiving() {
// given
let location = CLLocation.init(latitude: 0.0, longitude: 0.0)
let test = Test(location: location)
// when
let data = NSKeyedArchiver.archivedData(withRootObject: test)
let unarchivedTest = NSKeyedUnarchiver.unarchiveObject(with: data) as? Test
// then
XCTAssertEqual(unarchivedTest!.location, location, "location was not correctly unarchived")
}
此测试失败:
XCTAssertEqual failed: ("<+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 1/19/18, 3:30:50 PM Central European Standard Time") is not equal to ("<+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 1/19/18, 3:30:50 PM Central European Standard Time") - location was not correctly unarchived
对于原始位置和未存档位置,日志显示了两次完全相同的数据。
知道可能出了什么问题吗??
【问题讨论】:
-
我认为您在测试中将 CLLocation 与 ShoppingLocation 进行比较是不正确的吗?
-
哦,对不起,我简化了代码,忘记编辑了。我将编辑我的问题,感谢您指出这一点!
-
试试
let x = CLLocation(); let y = x.copy() as! CLLocation; XCTAssertEqual(x, y);。那也失败了。这可能是比编码更基本的问题。我打赌它会退回到NSObjectisEqual方法,只有当它们指向同一个实例时才会返回 true。 -
@Rob 你认为我应该提交错误报告吗?
-
他们可能会忽略“错误”报告,因为它似乎按记录工作。但也许是一个“增强”请求......
标签: ios unit-testing cllocation nskeyedarchiver nskeyedunarchiver