https://kotlinlang.org logo
#kotest
Title
# kotest
m

Mini

02/08/2022, 7:12 PM
Im not sure if this is a kotest problem, or my understanding of map/combine is lacking, but Im struggling to understand why combine does not work as expected in my tests… Basic example in 🧵
Copy code
class MyTest() : FunSpec({

    val dispatcher = TestCoroutineDispatcher()
    listeners(MainCoroutineListener(dispatcher), MtLogListener())

    class MyViewModel : ViewModel(){

        val x = MutableStateFlow(5)
        val y = MutableStateFlow(2)

        val doubleX = x.map {
            it * 2
        }.stateIn(
            viewModelScope,
            SharingStarted.Eagerly,
            null
        )

        val combinedFlow = x.combine(y){ xValue, yValue ->
            xValue * yValue
        }.stateIn(
            viewModelScope,
            SharingStarted.Eagerly,
            null
        )
    }

    val viewModel = MyViewModel()

    test("stateflow mapped"){
        viewModel.doubleX.value shouldBe 10
        viewModel.x.value = 9
        viewModel.doubleX.value shouldBe 18
    }

    test("combined"){
        viewModel.x.value = 10
        viewModel.y.value = 3
        viewModel.combinedFlow.value shouldBe 30
    }
}){
    override fun isolationMode(): IsolationMode = IsolationMode.InstancePerLeaf
}
first test passes, second fails 😕
s

sam

02/08/2022, 7:54 PM
do you mean to re-create the class for the 2nd test ?
m

Mini

02/08/2022, 9:00 PM
huh.. I didnt expect that to have any impact, however I also expected InstancePerLeaf to do that.. I guess that only applies to context / nested tests.. both instantiating inside each test case and wrapping the current setup in a context makes the test pass…
I might be even more confused now 😅 I’ll have to come back to this tomorrow. My real issue is not solved by instantiating inside the test case
7 Views