【问题标题】:test case using mockk in not working for function使用 mockk 的测试用例无法正常工作
【发布时间】:2020-03-21 12:31:15
【问题描述】:

我是 mockk 框架的新手,正在尝试为以下函数编写测试用例

    fun fromCityWeather(cityWeather: List<CityWeather>): HomeWeather {
        return HomeWeather( 
            cityWeather.first().temperature,
            cityWeather.first().description
        )
    }

CityWeather 和 HomeWeather 是我的 2 个类,它们都有温度和天气作为字段。这个函数写在另一个类的伴生对象中。

请帮助我理解开始的逻辑,因为我在参考互联网上的博客时多次尝试编写测试用例,但都没有成功。

【问题讨论】:

  • 你需要了解这段代码的逻辑,然后才能测试它吗?还是mock的逻辑?您不需要 mockk 来为这段代码编写单元测试,因为它会将一个对象映射到另一个对象,而无需模拟任何服务。

标签: android kotlin mockk


【解决方案1】:

你不需要 mock:

@Test
fun testFromCityWeather() {
    val weatherList = listOf(CityWeather("30", "degrees"), CityWeather("12", "degrees"))

    val expected = HomeWeather("30", "degrees")

    assertEquals(expected, fromCityWeather(weatherList))
}

(假设温度和描述是字符串)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2022-10-17
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多