jetpack-movie icon indicating copy to clipboard operation
jetpack-movie copied to clipboard

Spek Test failed after updating dependency

Open wiryadev opened this issue 2 years ago • 0 comments

Hi, i tried to fork this repo for learn testing. And after updated dependencies, the ViewModel test that using Spek always failed. I've made adjustment to threading in the InstantTaskExecutorRuleSpek so it wont fail because of coroutines again. But onChanged value always null in verify section.

InstantTaskExecutorRuleSpek

@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
class InstantTaskExecutorRuleSpek(root: Root) {
    private val dispatchers = newSingleThreadContext("test")
    init {
        root.beforeGroup {
            Dispatchers.setMain(dispatchers)
            ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
                override fun executeOnDiskIO(runnable: Runnable) {
                    runnable.run()
                }
                override fun isMainThread(): Boolean {
                    return true
                }
                override fun postToMainThread(runnable: Runnable) {
                    runnable.run()
                }
            })
        }
        root.afterGroup {
            ArchTaskExecutor.getInstance().setDelegate(null)
            Dispatchers.resetMain()
            dispatchers.close()
        }
    }
}

The test itself hasn't changed from the original except i wrap it in launch block now

When("getting popular movies") {
    runBlocking {
        launch(Dispatchers.Main) {
            Mockito.`when`(useCase.getPopularMovie()).thenReturn(returnValue)
            viewModel.getPopularMovie()
        }
    }
}

If you have spare time, i would love to see some assistance to help me overcome this :sweat_smile:

wiryadev avatar Dec 08 '21 08:12 wiryadev