简体   繁体   English

如何用 mockk 模拟密封类?

[英]How to mock sealed classes with mockk?

I am trying to mock a sealed class which looks something like this:我正在尝试模拟一个密封的 class,它看起来像这样:

sealed class Location

class Home: Location{
    val name = "Home"
}

I would like to be able to do the following:我希望能够做到以下几点:

val mockHome = mockk<Home>() {
    every { name } answers { "My Home" }
}

But unfortunately when I run this it fails with the following error:但不幸的是,当我运行它时它失败并出现以下错误:

io.mockk.MockKException: Missing calls inside every {... } block. io.mockk.MockKException:每个 {...} 块内缺少调用。

What am I doing wrong?我究竟做错了什么?

For now you cannot.现在你不能。 but this issue was fixed in this PR and we are waiting for next release of Mockk.但是这个问题在这个PR中已经修复,我们正在等待 Mockk 的下一个版本。 you can track this PR for more information您可以跟踪此 PR 以获取更多信息

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

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