简体   繁体   English

使用 mockk 的测试用例不适用于功能

[英]test case using mockk in not working for function

I am new to mockk framework and trying to write a test case for below function我是 mockk 框架的新手,并试图为以下函数编写测试用例

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

CityWeather and HomeWeather are my 2 classes and both have temperature and weather as fields. CityWeather 和 HomeWeather 是我的 2 个类,它们都将温度和天气作为字段。 This function is written inside companion object of another class.这个函数写在另一个类的伴生对象中。

Please help me to understand the logic to start with as I have tried multiple times writing the test case while referring the blogs on internet and none worked.请帮助我理解开始的逻辑,因为我在参考互联网上的博客的同时尝试了多次编写测试用例,但都没有奏效。

You don't need mockk:你不需要模拟:

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

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

    assertEquals(expected, fromCityWeather(weatherList))
}

(Assuming temperature and description are strings) (假设温度和描述是字符串)

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

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