0
我想用Kotlin + Spek框架編寫單元測試。到目前爲止看起來相當不錯,但我有一個問題,我必須編寫大量的樣板代碼,如果我想要測試一個類的幾個Spec對象。用Kotlin和Spek創建一個用於初始化共享對象的抽象Spek
這是我的代碼來說明這個問題:
object MyFirstSpec : Spek({
val myMock1: SomeObject1 = mock()
val myMock2: SomeObject2 = mock()
val myMock3: SomeObject3 = mock()
val myMock4: SomeObject4 = mock()
val myMock5: SomeObject5 = mock()
val myMock6: SomeObject6 = mock()
val myClass = MyClassToTest(myMock1, myMock2, myMock3, myMock4, myMock5, myMock6)
given("something to test here") {
//my test here
}
})
object MyOtherSpec : Spek({
val myMock1: SomeObject1 = mock()
val myMock2: SomeObject2 = mock()
val myMock3: SomeObject3 = mock()
val myMock4: SomeObject4 = mock()
val myMock5: SomeObject5 = mock()
val myMock6: SomeObject6 = mock()
val myClass = MyClassToTest(myMock1, myMock2, myMock3, myMock4, myMock5, myMock6)
given("something else to test here") {
// my test here
}
})
我很想創造「抽象」斯貝克,在那裏我將定義我的嘲笑等,並繼承後或在我的規格在某種程度上分享。
有沒有辦法做到這一點?