Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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用例和存储库单元测试_Android_Kotlin_Junit_Kotlin Coroutines_Android Mvvm - Fatal编程技术网

Android用例和存储库单元测试

Android用例和存储库单元测试,android,kotlin,junit,kotlin-coroutines,android-mvvm,Android,Kotlin,Junit,Kotlin Coroutines,Android Mvvm,我在单元测试用例时遇到问题。下面是用例代码- interface MyUseCase { fun performCustomAction(action: MyAction): LiveData<Result> } class MyUseCaseImpl(private val dataRepository: DataRepository) : MyUseCase { override fun performCustomActi

我在单元测试用例时遇到问题。下面是用例代码-

    interface MyUseCase {
        fun performCustomAction(action: MyAction): LiveData<Result>
    }

    class MyUseCaseImpl(private val dataRepository: DataRepository) : MyUseCase {

       override fun performCustomAction(action: MyAction) = liveData {
          emit(Result.DataFetchInProgress)
          emit(dataRepository.performDataOperation(action))
       }
    }
我在这里犯了什么错误


另外,您能建议我在我的用例中还可以测试什么吗?

我修复了它。基本上我需要添加一个Mockito。当…然后返回livedata。 下面是代码-

@Test
fun myUseCase_loginUser_expectedUserLoggedIn() {
    `when`(
        myUseCase.performCustomAction(
          MyAction.LoginUser(
            "userName", "password"
          )
        )
    ).thenReturn(liveData)

    val result = myUseCase.performCustomAction(
        MyAction.LoginUser(
            "userName", "password"
        )
    )

    Assert.assertThat(result, `is`(liveData))
}
java.lang.AssertionError: 
Expected :androidx.lifecycle.CoroutineLiveData@4c012563
Actual   :liveData
@Test
fun myUseCase_loginUser_expectedUserLoggedIn() {
    `when`(
        myUseCase.performCustomAction(
          MyAction.LoginUser(
            "userName", "password"
          )
        )
    ).thenReturn(liveData)

    val result = myUseCase.performCustomAction(
        MyAction.LoginUser(
            "userName", "password"
        )
    )

    Assert.assertThat(result, `is`(liveData))
}