I'm writing a unit test to check that my conversion from a date to string and back is successful.
I convert it to a string via:
func convertDateToString(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date)
}
and convert it back via:
func convertStringToDate(string: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.date(from: string)!
}
If you try to use the Equatable protocol on the pre-conversion date and post-conversion date it says they are not the same. However, if you convert both pre and post dates to strings and compare them, they are Equatable. This is what it says when I run XCAssertEqual on pre and post dates:
XCTAssertEqual failed: ("2020-01-22 19:35:40 +0000") is not equal to ("2020-01-22 19:35:40 +0000")
Which looks pretty identical to me. I even tried converting the pre-conversion date to a string and back to check if the dates were equal and they still weren't