swift - 如何在 Swift 中对日期进行单元测试
问题描述
如果我有这样的 ArticleItem 结构:
public struct ArticleItem: Equatable {
public let id: UUID
public let description: String?
public let location: String?
public let thumbnailURL: URL
public let created: Date
public init(id: UUID, description: String?, location: String?, thumbnailURL: URL, created: Date) {
self.id = id
self.description = description
self.location = location
self.thumbnailURL = thumbnailURL
self.created = created
}
}
但是从后端我们会收到这样的信息:
{
"id": "a UUID",
"description": "a description",
"location": "a location",
"image": "https://a-image.url",
"created_date": "2020-05-09",
}
如何测试 Date 的正确性?
在测试中我有这样的事情:
func test_load_deliversItemsOn200HTTPResponseWithJSONItems(){
let (sut, client) = makeSUT()
let item1 = ArticleItem(id: UUID(), description: nil, location: nil, thumbnailURL: URL(string: "http://a-url.com/")!, created: Date())
let item1JSON = [
"id": item1.id.uuidString,
"image": item1.imageURL.absoluteString,
"created_date": ??? // what should be here
]
let items = [
"items": [item1JSON]
]
expect(sut: sut, toCompleteWithResult: .success([item1])) {
let itemsJSON = try! JSONSerialization.data(withJSONObject: items)
client.complete(withStatusCode: 200, data: itemsJSON)
}
}
我听说过iso8601
解码时使用,但仍然不知道该怎么做。
请帮我。谢谢
解决方案
推荐阅读
- flutter - No Icon but it can be tested
- python - 使用 pandas.DataFrame.plot() 时如何显示所有 xticks?
- java - 有没有办法将外部文件“信息”保存到java中的数组中?
- wordpress - 插件 gatsby-source-wordpress 中的 ETIMEDOUT 错误
- c - 问题涉及N x N的网格,并且有一定数量的小偷和强盗。数一数有多少小偷被抓
- c# - CSVHelper 将值传递给 MapClass
- r - 测试某些变量(不同版本)是否出现在数据中
- wordpress - 自定义元框字段不显示 HTML
- xml - 如何使用 expath 和 Xquery 获取 CSV 文件?
- javascript - 由带有标题的图块组成的菜单——图像不会在 flex 中缩小