android - 测试协程和 LiveData : Coroutine result not reflected on LiveData

标签 android android-testing android-livedata coroutine

我是测试新手,我想学习如何使用 MVVM 模式测试协程。我刚刚关注了 https://github.com/android/architecture-samples 项目并做了一些更改(删除了远程源)。但是在测试 ViewModel 以从存储库中获取数据时,它一直失败并出现此错误。

value of    : iterable.size()
expected    : 3
but was     : 0
iterable was: []
Expected :3
Actual   :0

下面是我的 ViewModel 测试类,不知道我遗漏了什么。此外,在模拟存储库时,我可以在打印 taskRepository.getTasks() 时从中获得预期结果,它只是在调用 loadTasks( )

View 模型测试

@ExperimentalCoroutinesApi
@RunWith(MockitoJUnitRunner::class)
class TasksViewModelTest {

    private lateinit var tasksViewModel: TasksViewModel

    val tasksRepository = mock(TasksRepository::class.java)

    @ExperimentalCoroutinesApi
    @get:Rule
    var mainCoroutineRule = TestMainCoroutineRule()

    // Executes each task synchronously using Architecture Components.
    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setupViewModel() {
        tasksViewModel = TasksViewModel(tasksRepository)
    }

    @Test
    fun whenLoading_hasListOfTasks() = runBlockingTest {
        val task1 = Task("title1", "description1")
        val task2 = Task("title2", "description2")
        val task3 = Task("title3", "description3")
        `when`(tasksRepository.getTasks()).thenReturn(Result.Success(listOf(
           task1,
           task2,
           task3
        )))

        tasksViewModel.loadTasks()

        val tasks = LiveDataTestUtil.getValue(tasksViewModel.tasks)
        assertThat(tasks).hasSize(3)
    }
}

任务 View 模型

class TasksViewModel @Inject constructor(
  private val repository: TasksRepository
) : ViewModel() {

  private val _tasks = MutableLiveData<List<Task>>().apply { value = emptyList() }
  val tasks: LiveData<List<Task>> = _tasks

  fun loadTasks() {
    viewModelScope.launch {
      val tasksResult = repository.getTasks()

      if (tasksResult is Success) {
        val tasks = tasksResult.data
        _tasks.value = ArrayList(tasks)
      }
    }
  }
}

下面列出了帮助类,我只是从示例项目中复制了相同的类。

LiveDataTestUtil

object LiveDataTestUtil {

    /**
     * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
     * Once we got a notification via onChanged, we stop observing.
     */
    fun <T> getValue(liveData: LiveData<T>): T {
        val data = arrayOfNulls<Any>(1)
        val latch = CountDownLatch(1)
        val observer = object : Observer<T> {
            override fun onChanged(o: T?) {
                data[0] = o
                latch.countDown()
                liveData.removeObserver(this)
            }
        }
        liveData.observeForever(observer)
        latch.await(2, TimeUnit.SECONDS)

        @Suppress("UNCHECKED_CAST")
        return data[0] as T
    }
}

主协程规则

@ExperimentalCoroutinesApi
class TestMainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
    }
}

最佳答案

事实证明这是 mockito 的问题,我有一个旧版本,我发现有一个名为 mockito-kotlin 的库可以简化测试协程,如 here 所述.然后我将我的代码修改为这个,它运行良好。

tasksRepository.stub {
    onBlocking { getTasks() }.doReturn(Result.Success(listOf(task1, task2, task3)))
}

关于android - 测试协程和 LiveData : Coroutine result not reflected on LiveData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534129/

相关文章:

android - 在 AsyncTask 中测试一个涉及监听器回调的类

android - 在数据绑定(bind)中找不到属性的 setter

Android MVVM 模式 - 用户输入

没有 TwitterLoginButton 的 Android Fabric TwitterCore 登录

android - 根据当前位置获取我当前所在的总计 "Areas/Zones"

android - 检测 Marshmallow 中的 toast

Android Studio 无法识别 Espresso/Hamcrest 静态方法名称而不以类名开头

android - 是否有任何 Sqlite select 查询来获取两个表结果?

java - 如何在Android中正确使用snackbar

java - ViewModel 没有零参数构造函数错误 - 即使它具有零参数构造函数