如何存根 kotlin 映射函数 |单元测试

问题描述 投票:0回答:1

我想在我的单元测试中存根 kotlin 的地图扩展功能。在互联网上找不到任何合适的解决方案来做到这一点。

实际上我已经将空块放在大括号内,但似乎这不是我应该做的。

这是我到目前为止一直在尝试的:

whenever(trainWorkoutResult.exerciseSections.map {}).thenReturn(trainSections)

这是我得到的错误:

no answer found for: 

    LegacyDataMapper(#4).mappingSectionsToRecordedExerciseDataModels(Mock for List, hashCode: 1199894489)

有什么想法吗?

android kotlin unit-testing extension-methods extension-function
1个回答
0
投票

这样解决了:

 with(mockk<List<Section>>()) {
                every {
                    trainWorkoutResult.exerciseSections
                } returns (listOf(Section(12, "123", emptyList())))
            }

灵感来自this问题。

ps:我正在使用

mockk

© www.soinside.com 2019 - 2024. All rights reserved.