MockK 模拟 ViewModel 的 savedStateHandle.getLiveData()

MockK mock ViewModel's savedStateHandle.getLiveData()

我正在尝试使用 savedStateHandle 测试此 ViewModel

class PostListViewModel @ViewModelInject constructor(
    private val coroutineScope: CoroutineScope,
    private val getPostsUseCase: GetPostListUseCaseFlow,
    @Assisted savedStateHandle: SavedStateHandle
) : AbstractPostListVM() {

    private val _goToDetailScreen =
        savedStateHandle.getLiveData<Event<Post>>(POST_DETAIL)
//        MutableLiveData<Event<Post>>()

    override val goToDetailScreen: LiveData<Event<Post>>
        get() = _goToDetailScreen

    private val _postViewState =
        savedStateHandle.getLiveData<ViewState<List<Post>>>(POST_LIST)
//        MutableLiveData<ViewState<List<Post>>>()

    override val postViewState: LiveData<ViewState<List<Post>>>
        get() = _postViewState

    override fun getPosts() {

        getPostsUseCase.getPostFlowOfflineFirst()
            .convertToFlowViewState()
            .onStart {
                _postViewState.value = ViewState(status = Status.LOADING)
            }
            .onEach {
                _postViewState.value = it
            }
            .launchIn(coroutineScope)
    }

    override fun refreshPosts() {
        getPostsUseCase.getPostFlowOfflineLast()
            .convertToFlowViewState()
            .onStart {
                _postViewState.value = ViewState(status = Status.LOADING)
            }
            .onEach {
                _postViewState.value = it
            }
            .launchIn(coroutineScope)
    }

    override fun onClick(post: Post) {
        _goToDetailScreen.value = Event(post)
    }
}

测试

@Test
fun `given exception returned from useCase, should have ViewState ERROR offlineFirst`() =
    testCoroutineRule.runBlockingTest {

        // GIVEN
        every {
            savedStateHandle.getLiveData<ViewState<List<Post>>>(AbstractPostListVM.POST_LIST)
        } returns MutableLiveData()

        every {
            savedStateHandle.getLiveData<Event<Post>>(AbstractPostListVM.POST_DETAIL)
        } returns MutableLiveData()

        every {
            useCase.getPostFlowOfflineFirst()
        } returns flow<List<Post>> {
            emit(throw Exception("Network Exception"))
        }

        val testObserver = viewModel.postViewState.test()

        // WHEN
        viewModel.getPosts()

        // THEN
        testObserver
            .assertValue { states ->
                (states[0].status == Status.LOADING &&
                        states[1].status == Status.ERROR)
            }
            .dispose()

        val finalState = testObserver.values()[1]
        Truth.assertThat(finalState.error?.message).isEqualTo("Network Exception")
        Truth.assertThat(finalState.error).isInstanceOf(Exception::class.java)
        verify(atMost = 1) { useCase.getPostFlowOfflineFirst() }
    }

当我使用 MutableLiveData 而不是 SavedStateHandle.getLiveData() 进行此测试和其他测试时,此测试有效。

savedStateHandle 被模拟为

private val savedStateHandle: SavedStateHandle = mockk()

returns

io.mockk.MockKException: no answer found for: SavedStateHandle(#2).getLiveData(POST_LIST)

并且我将模拟的 savedStateHandle 更改为 relaxed = true

private val savedStateHandle: SavedStateHandle = mockk(relaxed = true)

基于此

现在 postViewState 的值没有改变并且测试失败

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

使用 SavedStateHandle 模拟和获取 LiveData 的正确方法是什么?

我已经找到 MockK 库的解决方案并且工作正常,

我按照下面的方式做了

val savedStateHandle = mockk<SavedStateHandle>(relaxed = true)

在上面一行中,我创建了我在 ViewModel 中使用的 savedStateHandle

@Test
fun `test something simple`() {
    val savedStateHandle = mockk<SavedStateHandle>(relaxed = true)
    val viewModel = MyViewModel(savedStateHandle)
    verify { savedStateHandle.set(MyViewModel.KEY, "Something") }
}

可能是因为我用的是junit5,选的答案不适合我。

如果它对某人有帮助,我所做的是用我需要的信息创建一个 SavedStateHandle 实例,而不是在测试的第一部分(在 Given/Arrange 部分中)模拟它然后初始化 ViewModel:

@Test
fun `whatever`() =
    coroutineRule.testDispatcher.runBlockingTest {
        val savedStateHandle = SavedStateHandle().apply {
            set(MyViewModel.KEY, "something")
        }
        val viewModel = PostListViewModel(coroutineScope, getPostsUseCase, savedStateHandle)

        ...
}