嘲笑私人财产与嘲笑引发了意外 [英] Mock private property with mockk throws an excpetion

查看:100
本文介绍了嘲笑私人财产与嘲笑引发了意外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kotlin中使用 mockk 进行测试.但是我似乎无法在间谍对象中覆盖私有属性.

I'm using mockk for my testing in kotlin. But I can't seem to override a private property in a spy object.

我有这个对象

private val driverMapSnapshotMap: MutableMap<Int, SnapshotImage> = mutableMapOf()

在我从事侦查的班级中

viewModel = spyk(DriverListViewModel(), recordPrivateCalls = true)

但是当我尝试用模拟值填充它时,会出现错误

But when I try to make it fill up with mock values I get an error

every {
    viewModel getProperty "driverMapSnapshotMap"
} returns(mapOf(1 to mockkClass(SnapshotImage::class)))

我得到的错误

io.mockk.MockKException: Missing calls inside every { ... } block.

有什么想法吗?

推荐答案

模拟私有属性几乎是不可能的,因为它们没有附加getter方法.这是Kotlin优化的一种,解决方案是主要的变化.

It is nearly impossible to mock private properties as they don't have getter methods attached. This is kind of Kotlin optimization and solution is major change.

这里有同样的问题:

https://github.com/mockk/mockk/issues/263

这篇关于嘲笑私人财产与嘲笑引发了意外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆