I'm trying to remove the `withContext` from my vi...
# coroutines
j
I'm trying to remove the
withContext
from my viewModel and I'm planning to put it on the
usecase
/
repository
something like this
Copy code
launch{
 updateView()
 withContext(<http://Dispatchers.IO|Dispatchers.IO>){
   doLogin(username,password)
 }
 updateView()
}
This is how I'm doing it now ^ Then if I want to remove the
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
I can do it like this
Copy code
suspend fun doLogin(usernarme: String, password: String) = <http://Dispatchers.IO|Dispatchers.IO> { repo.doLogin(username,password)... }
So, is there any way to test it correctly? I'm using
mockK
so, I know with
coEvery
is going to work, but is it safe to do not put
Dispatchers.Uncofined
for testing purposes? Also do you think it's a good idea to do not change context on viewModel and then just do this
Copy code
launch {
 updateView()
 doLogin(username,password)
 updateView()
}
s
Instead of hard-coding the Dispatchers.IO, inject the io-dispatcher. In your actual code, you'd inject the Dispatcher.IO, in your unit-tests you'd inject the Dispatchers.Unconfined or the TestCoroutineDispatcher.
đź‘Ť 1
j
I see your point, yes, that was my first idea, to create like a dispatcher provider, so I can mock it on my tests.
Is there a problem if I run the test with the
<http://Dispatchers.IO|Dispatchers.IO>
instead of the
Disptachers.Uncofined
?
t
Unconfined is for unit testing, you’d still be using IO for network or dB calls