iT邦幫忙

2023 iThome 鐵人賽

DAY 6
0
Mobile Development

在 iOS 專案上加上 Unit testing - 因為 You need testing系列 第 6

D6 - 在 iOS 專案加上測試-You need testing {從後端回來的 json 開始寫測試 part 2}

  • 分享至 

  • xImage
  •  

前一天的文章,我們快速的從 quicktype 網站拿到了 Decodable Model 程式碼,那就可以從 json response 和 Model 程式碼來進行測試。

Step1: 在 Test Target 新增一個 Test Class 並命名為 UserElementTests

記得在檔案創造出來後,將不需要的程式碼移除,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.
        }
    }

https://ithelp.ithome.com.tw/upload/images/20230917/20140622CunZC3WMVp.png

Step2: 在專案中建立 UserElement 檔案,並將 quicktype 產生的程式碼貼上。

// 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]

Step3: 在 Unit testing 程式中,產生 mock json data

在 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()
    }

Step4: 使用 @testable import YouNeedTesting 來引用專案中 access level 為 internal 的物件

import XCTest
@testable import YouNeedTesting // 要加這一行,不然是拿不到 internal 物件

Step5:

開始寫 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 運作正常

https://ithelp.ithome.com.tw/upload/images/20230917/20140622XbbrlwwhxS.png

修正後

func testUserElements() throws {
        
        let data = getRawData()
        let users = try JSONDecoder().decode(User.self, from: data)
        XCTAssertEqual(users.count, 3)
    }

https://ithelp.ithome.com.tw/upload/images/20230917/20140622Y9E0lzQNkF.png

這樣,就完成了第一筆測試。

不過……這個 data model 這樣就足夠了嗎?

剩下的部分,我們下一篇的文章進行解答。


上一篇
D5 - 在 iOS 專案加上測試-You need testing {從後端回來的 json 開始寫測試 part 1}
下一篇
D7 - 在 iOS 專案加上測試-You need testing {從後端回來的 json 開始寫測試 part 3}
系列文
在 iOS 專案上加上 Unit testing - 因為 You need testing32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言