Issue
Hello fellow developers,
i have a following problem:
Let's say I have a class called UseCaseX:
class UseCaseX (private val repository: Repository) {
override suspend fun invoke(params: Unit?) = repository.call()
}
Now, this UseCaseX is used in a class ViewModel, like this:
...
val result = async { useCaseX.invoke(null) }
...
Then I use MockK to mock the UseCaseX in a ViewModelTest, like this:
val useCaseX = mockk<UseCaseX>(relaxUnitFun = true)
And I get the following error:
io.mockk.MockKException: no answer found for: UseCaseX(#7).invoke(null, continuation {})
at io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:90)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
So obviously the UseCaseX.invoke(params)
function gets extended during async{}
with an additional parameter and there is no answer for it so far.
Question: how can I provide the answer for the mock in that case?
Solution
use withContext(Dispatchers.Default)
instead of async
. Should work!
Answered By - Flo We