前一天的文章,我們快速的從 quicktype 網站拿到了 Decodable Model 程式碼,那就可以從 json response 和 Model 程式碼來進行測試。
記得在檔案創造出來後,將不需要的程式碼移除,testExample() 和 testPeformanceExample() 是不需要的。
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
// MARK: - UserElement
struct UserElement: Codable {
let id: Int
let name, username, email: String
let address: Address
let phone, website: String
let company: Company
}
// MARK: - Address
struct Address: Codable {
let street, suite, city, zipcode: String
let geo: Geo
}
// MARK: - Geo
struct Geo: Codable {
let lat, lng: String
}
// MARK: - Company
struct Company: Codable {
let name, catchPhrase, bs: String
}
typealias User = [UserElement]
在 UserElementTests 寫一個 func 產出和前一天文章相同的 json data,你可以使用 #””” 與 “””# 將 json 格式的 string 包住。前後夾了相同數量的 #,可以不用在雙引號前面加上脫逸符號,這樣在複製 json 時,方便很多。
/// 這邊只取前三筆資料拿來做測試
private func getRawData() -> Data {
let string =
#"""
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"email": "Nathan@yesenia.net",
"address": {
"street": "Douglas Extension",
"suite": "Suite 847",
"city": "McKenziehaven",
"zipcode": "59590-4157",
"geo": {
"lat": "-68.6102",
"lng": "-47.0653"
}
},
"phone": "1-463-123-4447",
"website": "ramiro.info",
"company": {
"name": "Romaguera-Jacobson",
"catchPhrase": "Face to face bifurcated interface",
"bs": "e-enable strategic applications"
}
}
]
"""#
return string.data(using: .utf8) ?? Data()
}
import XCTest
@testable import YouNeedTesting // 要加這一行,不然是拿不到 internal 物件
開始寫 test,將 func 宣告為 func testFirstUserElement()
,並開始寫測試。記得,第一次跑要讓 test failed,然後再修正
func testUserElements() throws {
let data = getRawData()
let users = try JSONDecoder().decode(User.self, from: data)
XCTAssertEqual(users.count, 0)
}
修正前,確保 Unit testing 運作正常
修正後
func testUserElements() throws {
let data = getRawData()
let users = try JSONDecoder().decode(User.self, from: data)
XCTAssertEqual(users.count, 3)
}
這樣,就完成了第一筆測試。
不過……這個 data model 這樣就足夠了嗎?
剩下的部分,我們下一篇的文章進行解答。