Problem with testing a method in ViewModel

36 Views Asked by At

I have a method in a ViewModel that I need to test. I use Mockk library for testing and in this test, I need to make sure that useCase methods are called in a ViewModel. This is a method in ViewModel:

fun startCalculation(dataSize: Int) {
    job = CoroutineScope(Dispatchers.IO).launch {
        val arrayListResultList = arrayListPerformanceUseCase.testArrayList(dataSize)
        arraylistResultListFlow.emit(arrayListResultList)

        val linkedListResultList = linkedListPerformanceUseCase.testLinkedList(dataSize)
        linkedlistResultListFlow.emit(linkedListResultList)

        val copyOnWriteArrayListResultList = copyOnWriteArrayListPerformanceUseCase.testCopyOnWriteArrayList(dataSize)
        copyOnWriteArrayListResultListFlow.emit(copyOnWriteArrayListResultList)
    }
}

This is what I tried to write

@Test
fun testStartCalculation() = runBlocking {
    // Mock the behavior of your use cases
    coEvery { getArrayListPerformanceUseCase.testArrayList(any()) } returns emptyList()
    coEvery { getLinkedListPerformanceUseCase.testLinkedList(any()) } returns emptyList()
    coEvery { getCopyOnWriteArrayListPerformanceUseCase.testCopyOnWriteArrayList(any()) } returns emptyList()

    // Call the function under test
    collectionViewModel.startCalculation(1000)

    // Verify that the use case methods are called with the expected argument
    coVerify { getArrayListPerformanceUseCase.testArrayList(1000) }
    coVerify { getLinkedListPerformanceUseCase.testLinkedList(1000) }
    coVerify { getCopyOnWriteArrayListPerformanceUseCase.testCopyOnWriteArrayList(1000) }
}

But each time I try to run this test I get the following error. Could you please help me to understand what is wrong? Maybe the test itself should be changed somehow?

enter image description here

0

There are 0 best solutions below