Hi there! I am new to coroutines. I try to test a ...
# test
u
Hi there! I am new to coroutines. I try to test a
ViewModel
in my app. Having the following
BaseUseCase
class:
Copy code
abstract class BaseUseCase<out Type, in Params> where Type : Any {

    abstract suspend fun run(params: Params): Either<Throwable, Type>

    open operator fun invoke(
        scope: CoroutineScope,
        params: Params,
        onResult: (Either<Throwable, Type>) -> Unit = {}
    ) {
        val backgroundJob = scope.async { run(params) }
        scope.launch { onResult(backgroundJob.await()) }
    }
}
And a concrete use case implementation:
Copy code
open class SetupWallet(
    private val repository: AuthRepository
) : BaseUseCase<Unit, SetupWallet.Params>() {

    override suspend fun run(params: Params) = try {
        val wallet = repository.createWallet(
            path = params.path
        )
        repository.saveMnemonic(wallet.mnemonic).let {
            Either.Right(it)
        }
    } catch (e: Throwable) {
        Either.Left(e)
    }

    class Params(val path: String)
}
I have trouble testing
ViewModel
executing
SetupWallet
. Here method that is being under test:
Copy code
fun onSignUpClicked() {
        setupWallet.invoke(
            scope = viewModelScope,
            params = SetupWallet.Params(
                path = pathProvider.providePath()
            )
        ) { result ->
            result.either(
                fnL = {
                    throw IllegalStateException()
                },
                fnR = { navigation.accept(NavigationCommand.OpenCreateProfile) }
            )
        }
    }
When I run the following test:
Copy code
@Test
    fun `when sign-up button clicked, should setup wallet`() {

        val path = "path"

        stubProvidePath(path)

        setupWallet.stub {
            onBlocking { run(params = any()) } doReturn Either.Left(Exception("!"))
        }

        vm.onSignUpClicked()
    }
Stubbing
SetupWallet
behavior has no effect. I expected that
IllegalStateException
will be thrown. But it does not 😞 Any help would be appreciated 🙏.