ubu
10/02/2019, 9:49 AMViewModel in my app. Having the following BaseUseCase class:
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:
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:
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:
@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 🙏.