0

I am new to mockk framework and trying to write a test case for below function

    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. 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.

  • Do you need to understand the logic of this code, before you can test it? Or the logic of mockk? You shouldn't need mockk to write unit tests for this code, as it is mapping one object to another with no need to mock any services. – DJ_Polly Mar 26 '20 at 03:31

1 Answers1

0

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)

DJ_Polly
  • 434
  • 3
  • 12