How to resolve error in unit testing when we have Date comparison in Codable

前端 未结 1 474
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 06:07

I tried to use custom date strategy in json decoding using Codable as describe in https://stackoverflow.com/a/28016692/3477974 . Here is the simplified implemen

1条回答
  •  悲&欢浪女
    2021-01-17 06:46

    What you are testing is the date/string conversion so there is no need to involve JSON encoding and decoding into this. Start with a fixed date in string form, convert it to date and back and check the end result vs the fixed date

    let input = "2020-02-02T19:10:23.123Z"
    
    let formatter = DateFormatter.iso8601
    let date = formatter.date(from: input)
    let output = date?.iso8601
    
    print(date != nil && input == output!)
    

    Note that this was written in a playground and not as a unit test

    0 讨论(0)
提交回复
热议问题