https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Joan Colmenero

03/03/2020, 10:12 AM
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

streetsofboston

03/03/2020, 12:35 PM
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

Joan Colmenero

03/03/2020, 2:00 PM
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

tjohnn

03/03/2020, 7:45 PM
Unconfined is for unit testing, you’d still be using IO for network or dB calls
6 Views