【发布时间】:2021-12-05 03:44:37
【问题描述】:
模拟类定义如下:
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
【问题讨论】:
-
你有你正在尝试的测试并抛出错误吗?在我的测试中,如果我打开
SomeClass,那么当我只模拟thing.somethingCommon >> "blah"时它就可以工作了
标签: kotlin groovy mocking spock