4
我無法添加單元測試我的雨燕的項目,所以我創建了一個新的Xcode的測試項目與我的類的精簡版本:如何在Swift中設置單元測試?
class SimpleClass {
let x: String
init(x: String) {
self.x = x
}
convenience init(dict: Dictionary<String, String>) {
self.init(x: dict["x"]!)
}
}
然後我創建了一個簡單的測試案例:
import XCTest
import TestProblem
class TestProblemTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
XCTAssertEqual(SimpleClass(x: "foo"), SimpleClass(dict: ["x": "foo"]))
}
}
我不得不進口項目本身(import TestProblem
)來解決懸而未決的標識符錯誤的SimpleClass
。
然而,當我嘗試運行測試,我得到以下編譯器錯誤:
Could not find an overload for 'init' that accepts the supplied arguments
缺少什麼我在這裏?即使在測試文件中,對初始化的調用也可以在XCTAssertEqual
調用之外正常工作。
在一種預感,我也試過:
let x = SimpleClass(x: "foo")
let y = SimpleClass(dict: ["x": "foo"])
XCTAssertEqual(x, y)
當我這樣做,我得到這個錯誤:
Cannot convert the expression's type 'Void' to type 'SimpleClass'
嗯,這可以解決問題。我想知道爲什麼明確的註釋是必要的。 – Bill
你是對的,通常它必須沒有明確的註釋。這對我來說看起來像一個bug,所以你可以報告給蘋果。 Xcode在beta2中,所以它還不完美。 – jaumard
感謝您的快速回答 - 我會報告問題。 – Bill