模擬類定義如下:
interface SomeInterface {
val somethingCommon: String
}
class SomeClass(val somethingSpecific: String) : SomeInterface {
override val somethingCommon: String
get() = somethingSpecific
}
被測代碼模擬 SomeClass 并在內部使用特定屬性和公共介面。不幸的是,僅模擬特定屬性并不能模擬相關的介面方法,因此似乎有必要同時模擬兩者:
def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> "blah"
在 Kotlin/Groovy/Spock 中有沒有辦法避免必須存根這兩種方法?我想出的最好的方法是將一個存根與另一個存根,這是可行的,但很不幸:
def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> thing.somethingSpecific
uj5u.com熱心網友回復:
您所描述的行為是Spy,如果您未指定任何回傳值,則Mock將回傳。null
Spock 只能模擬非最終類/方法,但您可以使用https://github.com/joke/spock-mockable動態打開它們進行測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/324548.html
上一篇:你如何在Kotlin中定義一組funs(functions)?
下一篇:Ktor默認池
