【问题标题】:Can I mock a generic (template) private method using MockK in Kotlin?我可以在 Kotlin 中使用 MockK 模拟通用(模板)私有方法吗?
【发布时间】:2018-04-24 11:09:52
【问题描述】:

我想模拟以下函数:

private fun <T> updateItemInDb(id: Long, column: String, data: T)

我的班级以以下方式调用它:

updateItemInDb(it, DB_POS, i),其中itLongDB_POSStringiInt

我希望该函数无需执行任何操作即可运行。我在单元测试中尝试了以下内容:

1) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } just Runs

这给了我一个类型不匹配的错误:required MockKStubScope<Unit>, found MockKStubScope<Any?>

2)every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } answers { }

这在运行时失败,io.mockk.MockKException: can't find function updateItemInDb(-1078155520644112829, -d008fa83c4f49c0, 843241211) for dynamic call

【问题讨论】:

  • 我为它创建了一个问题#70
  • 版本 1.7.16 应该可以解决这个问题。请检查并关闭 GH 问题 #70 如果它正在工作。关闭后(如果是),我会写下这个问题的答案。

标签: unit-testing kotlin mocking mockk


【解决方案1】:

现在是的。通用私有函数自 1.7.16 起已修复

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2021-03-05
    • 1970-01-01
    • 2022-09-27
    • 2022-11-27
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多