Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用RxJava2和MutableLiveData进行单元测试_Android_Unit Testing_Junit_Kotlin_Rx Java2 - Fatal编程技术网

Android 使用RxJava2和MutableLiveData进行单元测试

Android 使用RxJava2和MutableLiveData进行单元测试,android,unit-testing,junit,kotlin,rx-java2,Android,Unit Testing,Junit,Kotlin,Rx Java2,我试图对一些使用RxJava2的Android/Kotlin代码进行单元测试。代码大致如下所示: val temperature = MutableLiveData<Double>() fun save(): Single<Result<ClassifyResponse>> { val temp = temperature.value ... return repository.classify(request) .f

我试图对一些使用RxJava2的Android/Kotlin代码进行单元测试。代码大致如下所示:

val temperature = MutableLiveData<Double>()

fun save(): Single<Result<ClassifyResponse>> {
    val temp = temperature.value
    ...
    return repository.classify(request)
        .flatMap { response->
                val result: Result<ClassifyResponse> = Result.Success(response)
                Single.just(result)
        }.onErrorReturn {
                Result.Error(it)
        }
}
每次我运行单元测试时。我读了很多书,但即使在使用了RxJavaPlugins之后,我仍然无法模拟主线程调度程序,同样的异常仍然存在

有没有人可以看看我的单元测试课,并提出我在这里遗漏了什么

class MyViewModelTest() {

    private val repository = mock(MyRepository::class.java)

    private val immediateScheduler = object : Scheduler() {
        override fun createWorker(): Worker {
            return ExecutorScheduler.ExecutorWorker(Executor { it.run() })
        }
    }

    @Before
    fun setUp() {
        RxJavaPlugins.setInitIoSchedulerHandler { immediateScheduler }
        RxJavaPlugins.setComputationSchedulerHandler { immediateScheduler }
        RxAndroidPlugins.setInitMainThreadSchedulerHandler { immediateScheduler }
        RxAndroidPlugins.setInitMainThreadSchedulerHandler { immediateScheduler }
    }

    @Test
    fun test() {
        // given temperature view model
        val viewModel = MyViewModel(repository)
        ...

        val classifyResponse = ClassifyResponse(...)
        `when`(repository.classify(any(ClassifyRequest::class.java))).then { Single.just(classifyResponse) }

        // when sending request
        val result = viewModel.save()

        // then verify result
        val expected = Result.success(classifyResponse)

        assertEquals(expected, result)
    }
}

编辑:我注意到它由于使用
MutableLiveData
而失败。当我尝试访问
temperature.value
时,它会抱怨MainLooper线程。

您的RxJava配置正确,对于测试
MutableLiveData
,您需要在测试中设置规则:

@Rule
@JvmField
val rule = InstantTaskExecutorRule()

哪里来自module
android.arch.core:core testing
(很快就要迁移到AndroidX了)——目前最新版本是1.1.1

看来你的测试一切都是正确的。你能展示更多的代码吗,尤其是那些乱七八糟的线程代码?我刚刚注意到这是由于在我的viewModel(和test)中使用了MutableLiveData造成的。这方面也有解决办法吗?
@Rule
@JvmField
val rule = InstantTaskExecutorRule()