You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are trying to check that the scope is canceled on a Test.
We have injected via constructor the scope and dispatcher, for instance MainScope() and Dispatchers.IO, this works good, now we want to ensure that when we call one method the scope is canceled.
Example code
class ExampleClass @Inject constructor(val scope: CoroutineScope, val dispatcher: CoroutineDispatcher, val service: MyService){
fun example(){
scope.launch{withContext(dispatcher){service.getExamples()}.fold{..},{..})
}
fun methodToTest(){
scope.cancel()
}
}
Note: this service is a fake API call, which requires network operations, we can test the example, but when trying to test the methodToTest which contains the scope.cancel() is not working for us.
Problem?
On test, we are using
val dispatcher = TestCoroutineDispatcher()
val scope = TestCoroutineScope(dispatcher)
It works, but when trying to use this
scope.coroutineContext.isCanceled
It throws
Scope cannot be canceled because it does not have a Job
Investigating a little bit, we noticed that TestCoroutineScope is creating a default context EmptyCoroutineContext and the problem is that This CoroutineContext doesn't have a Job() so it's not possible to check if it's canceled or not.
What we can do and actually fixes the problem is to initialize the scope as
val scope = TestCoroutineScope(Job() + dispatcher)
It solves the problem, but we think that it's not the proper way because is using the "real" implementation of a Job() and there are a lot of unnecessary things that we don't need.
Is there any way to test it without passing a real Job()?
The text was updated successfully, but these errors were encountered:
We are trying to check that the scope is canceled on a Test.
We have injected via constructor the scope and dispatcher, for instance
MainScope()
andDispatchers.IO
, this works good, now we want to ensure that when we call one method the scope is canceled.Example code
Note: this service is a fake API call, which requires network operations, we can test the example, but when trying to test the methodToTest which contains the scope.cancel() is not working for us.
Problem?
On test, we are using
It works, but when trying to use this
It throws
Investigating a little bit, we noticed that
TestCoroutineScope
is creating a default contextEmptyCoroutineContext
and the problem is that ThisCoroutineContext
doesn't have aJob()
so it's not possible to check if it's canceled or not.What we can do and actually fixes the problem is to initialize the scope as
It solves the problem, but we think that it's not the proper way because is using the "real" implementation of a
Job()
and there are a lot of unnecessary things that we don't need.Is there any way to test it without passing a real
Job()
?The text was updated successfully, but these errors were encountered: