我開始用BDD方法編寫iOS單元測試。我有一個關於guard
聲明並獲得100%代碼覆蓋率的問題。如何獲得Guard守護進程的測試覆蓋率落後
我有以下代碼,該代碼處理將Data
轉換爲Customer
對象。
internal final class func customer(from data: Data) -> Customer? {
do {
guard let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: []) as? Dictionary<String, Any> else {
return nil
}
var customerFirstName: String? = nil
var customerLastName: String
if let firstName = jsonDictionary["first_name"] as? String {
customerFirstName = firstName
}
guard let lastName = jsonDictionary["last_name"] as? String else {
return nil
}
customerLastName = lastName
return Customer(firstName: customerFirstName, lastName: customerLastName)
} catch {
return nil
}
}
當我們的後端被創建時,一些客戶被給了一個姓氏,其中包含他們的名字和姓氏。這就是爲什麼客戶的名字是可選的;他們的全名可能是last_name
的值。
在我的代碼中,客戶的名字是可選的,而他們的姓是必需的。如果他們的姓氏未從網絡請求收到的JSON中返回,則我不創建該客戶。此外,如果Data
無法序列化爲Dictionary
,則不會創建客戶。
我有兩個JSON文件,它們都包含用於測試這兩種情況的客戶信息。
一個包含了JSON沒有名字:
{
"first_name": null,
"last_name": "Test Name",
}
其他包含在JSON第一個名字:
{
"first_name": "Test",
"last_name": "Name",
}
在我的單元測試,使用快速和靈活,我處理創建一個Customer
時,第一個名稱不可用,當它是:
override func spec() {
super.spec()
let bundle = Bundle(for: type(of: self))
describe("customer") {
context("whenAllDataAvailable") {
it("createsSuccessfully") {
let path = bundle.path(forResource: "CustomerValidFullName", ofType: "json", inDirectory: "ResponseStubs")!
let url = URL(fileURLWithPath: path)
let data = try! Data(contentsOf: url)
let customer = DataTransformer.customer(from: data)
expect(customer).toNot(beNil())
}
}
context("whenMissingLastName") {
it("createsUnsuccessfully") {
let path = bundle.path(forResource: "CustomerMissingLastName", ofType: "json", inDirectory: "ResponseStubs")!
let url = URL(fileURLWithPath: path)
let data = try! Data(contentsOf: url)
let customer = DataTransformer.customer(from: data)
expect(customer).to(beNil())
}
}
}
}
這可確保在第一個名稱丟失或存在於返回的JSON中時創建Customer
。
由於數據能夠轉換爲有效的JSON對象,因此當我的代碼未碰到guard
語句的else
子句時,如何使用BDD獲得此方法的100%代碼覆蓋率?我應該只添加另一個.json
文件,其中包含無法轉換爲JSON對象的數據以確保沒有創建Customer
以及包含缺少last_name
的.json
文件以確保未創建Customer
?
我是否只是過度思考「100%代碼覆蓋率」概念?我甚至需要測試guard
語句的else
條款嗎?我是否有使用BDD方法的適當方法?
@JonRein我很感謝您的見解!我可以問,你是如何得出結論的:我事後寫了我的測試?這正是我所做的,但我很好奇你是怎麼得出這個結論的。我肯定在研究你的概念,希望能夠更好地理解。 –
@NickKohrn因爲當代碼是測試驅動的時候,你不會寫任何不滿足測試的代碼。所以你最終不會問:「我有一些代碼,現在我該如何覆蓋它?」 –