简体   繁体   English

如何用 val 模拟() class

[英]How to mockk() class with val

class Totals {
    val subTotals = mutableMapOf<String, SubTotals >()
}
data class SubTotals(
    val x: Int = 0, 
    val y: Int = 0
)
every { getTotals() } returns Totals(
      subTotals = mutableMapOf("Bag" to SubTotals(10, 100)))

When I try to mockk() Totals I get cannot find a parameter with this name 'subTotals'当我尝试 mockk() Totals 时,我找不到名为“subTotals”的参数

You should move subTotals to constructor您应该将 subTotals 移动到构造函数

From

class Totals {
    val subTotals = mutableMapOf<String, SubTotals>()
}

to

class Totals(
    val subTotals: MutableMap<String, SubTotals> = mutableMapOf()
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM