java.lang.IllegalStateException: 取消调用不能没有可能运行

11

我想要用 Robolectric 测试一个简单的 ViewModel

这是我的 ViewModel

GreetingsViewModel.kt

@FlowPreview
@ExperimentalCoroutinesApi
class GreetingsViewModel : ViewModel() {


    private val greetingsChannel = ConflatedBroadcastChannel<String>()

    /**
     * Whenever greetingsChannel offers a value, prepend `Hello ` with it and return.
     */
    val greetingsResponse = greetingsChannel.asFlow()
        .flatMapLatest { name ->
            flowOf("Helo $name")
        }.asLiveData(viewModelScope.coroutineContext)

    /**
     * To get new greetings
     */
    fun askGreetings(name: String) {
        greetingsChannel.offer(name)
    }
}

这是我的单元测试文件:

GreetingsViewModelTest.kt

@FlowPreview
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class GreetingsViewModelTest2 {

    @Test
    fun greeting_askGreetings_getWithName() {
        val testViewModel = GreetingsViewModel()
        testViewModel.askGreetings("john")
        testViewModel.greetingsResponse.getOrAwaitValue().should.equal("Hello john")
    }
}

但是,当我运行测试时,出现以下错误。

java.lang.IllegalStateException: Cancel call cannot happen without a maybeRun

    at androidx.lifecycle.BlockRunner.cancel(CoroutineLiveData.kt:184)
    at androidx.lifecycle.CoroutineLiveData.onInactive(CoroutineLiveData.kt:245)
    at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:440)
    at androidx.lifecycle.LiveData.observeForever(LiveData.java:232)
    at com.theapache64.twinkill.test.GetOrAwaitValueKt.getOrAwaitValue(getOrAwaitValue.kt:26)
    at com.theapache64.twinkill.test.GetOrAwaitValueKt.getOrAwaitValue$default(getOrAwaitValue.kt:15)
    at com.theapache64.tracktor.ui.activities.test.GreetingsViewModelTest.greeting_askGreetings_getWithName(GreetingsViewModelTest.kt:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

有任何想法为什么吗?

注意:getOrAwaitValue() 是一个扩展函数,用于获取 LiveData 的值或等待其具有值,并带有超时时间。


有找到任何解决方案吗? - Ahmet Zorer
3个回答

3

3

我也遇到了同样的问题。正如@Rahul Singhal所指出的,错误的原因是这一行:

    this@getOrAwaitValue.removeObserver(this)

所以我决定将LiveData转换回Flow并获取其值。这是我现在用于获取值的扩展。

    suspend fun <T> LiveData<T>.getFlowAsLiveDataValue(timeMillis: Long = 2_000): T? =
        withTimeoutOrNull(timeMillis) {
        this@getFlowAsLiveDataValue.asFlow().first()
    }

您的测试应该长这样:
    testViewModel.greetingsResponse.getFlowAsLiveDataValue().should.equal("Hello john")

0
尝试下面的代码。
class GreetingsViewModelTest {

@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()


@Test
fun greeting_askGreetings_getWithName() {
    kotlinx.coroutines.Dispatchers.setMain(TestCoroutineDispatcher())
    val testViewModel = GreetingsViewModel()
    testViewModel.askGreetings("john")
    var name: String? = null
    testViewModel.greetingsResponse.observeForever {
        name = it
    }
    assertEquals("Helo john", name)
}

}

或者移除 this@getOrAwaitValue.removeObserver(this),两种方式都可以工作。

`


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接