https://kotlinlang.org logo
Title
a

audriusk

01/31/2020, 3:57 PM
I have test which pass successfully (expectation would be that it fails).
@Test
    fun someTest() = runBlockingTest {
        Dispatchers.setMain(coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)

        val viewModel = arrange()

        viewModel.viewModelScope.launch {
            throw IllegalStateException("mm")
        }
    }
From my understanding problem is
viewModelScope
extension which creates
SupervisorJob
at https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-viewmodel-ktx/src/main/java/androidx/lifecycle/ViewModel.kt;l=42;drc=9f40e609cef60d1889e2b13d48ba1dbb8aa345e3 What would be appropriate way to make such test method fail?
a

audriusk

01/31/2020, 5:38 PM
I am already using it. This line gets it
coroutineContext[ContinuationInterceptor] as CoroutineDispatcher
t

tseisel

01/31/2020, 5:50 PM
Is there a reason why you are retrieving the scope associated with
runBlockingTest
and not using an explicit
TestCoroutineDispatcher
like in the guide ?
a

audriusk

01/31/2020, 5:52 PM
In my tests I am using it in similar way. Just added everything to single test method for simplicity. Using Example solution gives same result
a

aipok

02/03/2020, 8:56 AM
I tried this myself and it looks like
runBlockingTest
has a bug still that mentioned https://github.com/Kotlin/kotlinx.coroutines/issues/1204 At least looks very similar. Usually for testing ViewModel classes I'm using injected
CoroutineContext
this allow me to be sure that view model is using Test Dispatcher.
a

audriusk

02/03/2020, 10:32 AM
I don't think that it is related. I solved that myselft. Related issue is https://github.com/Kotlin/kotlinx.coroutines/issues/1205