当我想模拟数据并测试 UI 片段时,doNothing() 不起作用

doNothing() does not work when i want to mock data and test UI Fragment(当我想模拟数据并测试 UI 片段时,doNothing() 不起作用)
本文介绍了当我想模拟数据并测试 UI 片段时,doNothing() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我要用 Espresso test fragment 然后我想 mock viewmodels 和成员.

I am going to test fragment with Espresso then i want to mock viewmodels and members.

在我的 viewModel 我有一个 void function 像这样:

In my viewModel i have a void function like this :

fun getLoginConfig() {
    viewModelScope.launchApiWith(_loginConfigLiveData) {
       repository.getLoginConfig()
    }
}

在测试 fragment 当我们从 viewModel 调用 getLoginConfig() 我想用 mock>doNothing() 但我面临这个错误:

In test fragment when we call getLoginConfig() from viewModel i want to mock it with doNothing() but i faced with this error :

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here


E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed

testFragmentClass 的这一行:

   @Before
    fun setUp() {
        //logOut


        mockVm = mock(SplashVM::class.java)

        loadKoinModules(module {
            single {
                mockVm
            }
        })
}




   doNothing().`when`(mockVm.getLoginConfig()).let {
       mockVm.loginConfigLiveData.postValue(Resource.Success(
           LoginConfigResponse(
               listOf("1"),1,1,"1",true)
       ))
   }

推荐答案

一些事情:

  • doNothing 什么都不做,这对于 mock 上的 void 方法来说是不必要的.这是默认行为.您只希望 doNothing 用于间谍或已存根的模拟.
  • 如果您希望在响应模拟调用时发生特定的事情,doAnswer 就是这样去.
  • doVerb 语法中,Mockito 期望那里只有一个变量;表达式不应调用 mock 上的方法,否则 Mockito 会认为您已经失去兴趣并抛出 UnfinishedStubbingException.
  • doNothing just does nothing, which is unnecessary for void methods on a mock. It's the default behavior. You only want doNothing for spies or already-stubbed mocks.
  • If you want something specific to happen in response to a call on a mock, doAnswer is the way to go.
  • In doVerb syntax, Mockito expects that there is only a variable there; the expression should not call a method on a mock, or else Mockito thinks you've lost interest and throws UnfinishedStubbingException.

因此你的修复看起来像:

Therefore your fix looks like:

doAnswer {
  mockVm.loginConfigLiveData.postValue(Resource.Success(
    LoginConfigResponse(
      listOf("1"),1,1,"1",true)
  ))
}.`when`(mockVm).getLoginConfig()

这篇关于当我想模拟数据并测试 UI 片段时,doNothing() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Android Mocking a Dagger2 injected dependency for a Espresso test(Android 为 Espresso 测试模拟 Dagger2 注入依赖项)
Mock object in Android Unit test with kotlin - any() gives null(使用 kotlin 在 Android 单元测试中模拟对象 - any() 给出 null)
How to mock Context using Mockito?(如何使用 Mockito 模拟上下文?)
Unit test Android, getString from resource(单元测试Android,从资源中获取字符串)
Mockito - what does verify method do?(Mockito - 验证方法有什么作用?)
Unit Testing in Retrofit for Callback(回调改造中的单元测试)