我正在编写一些测试来查看人员对象是否正确保存到本地数据库中(SQLite,使用字符串作为日期)。
在我的测试中,我有:
let testPerson = Person(firstName:"John", lastName:"Doe")
保存到数据库后,我测试是否 createdAt != nil
那没问题!然后 :
let testPerson2 = Person.LoadByID(testPerson.id)
这是一个静态函数,将数据库中的人员对象加载到 testPerson2
所有字段都在测试中通过(因此它们对于 testPerson 和 testPerson2 完全相同,除了 createdAt 和 updateAt 日期
如果我对这些日期进行 println,它们完全相同:
testPerson.createdAt : Optional(2015-08-14 12:03:01 +0000)
testPerson2.createdAt :Optional(2015-08-14 12:03:01 +0000)
但是测试(通过2种不同的方法):
XCTAssert(testPerson2.createdAt == testPerson.createdAt, "createdAt should be the same")
或者
XCTAssertTrue(testPerson2.createdAt!.isEqualToDate(testPerson2.createdAt!), "createdAt should be the same")
失败
使用 2x testPerson 或 testPerson2 进行这些测试,成功
我还尝试了以下代码:
if testPerson.createdAt!.compare(testPerson.createdAt!) == NSComparisonResult.OrderedDescending
{
println("date1 after date2");
} else if testPerson.createdAt!.compare(testPerson.createdAt!) == NSComparisonResult.OrderedAscending
{
println("date1 before date2");
} else
{
println("dates are equal");
}
如果我使用 testPerson 和 testPerson2 失败,使用 testPerson/testPerson 和 testPerson2/testPerson2 成功
为什么 ?Println 并查看数据库本身看起来没问题。