简体   繁体   English

通过单元测试如何测试 viewModel 中的 switchMap 从存储库类调用正确的方法

[英]With unit testing how to test a switchMap in viewModel calls the proper method from repository class

In my ViewModel i have used swithcMap with a liveData varaible as parameter.在我的 ViewModel 中,我使用了带有 liveData 变量作为参数的 swithcMap。 When the liveData variable gets set/changed then the switchMap calls a method from my Repository.当 liveData 变量被设置/更改时,switchMap 从我的存储库调用一个方法。 My viewModel code:我的视图模型代码:

class MainViewModel @Inject constructor(val mainRepository: MainRepository) : ViewModel() {

    var searchQuery:MutableLiveData<SearchQuery> = MutableLiveData()
    var liveResult:MediatorLiveData<My_Result> = MediatorLiveData()


    var apiData:LiveData<My_Result> = Transformations
    .switchMap(searchQuery){query ->
        query?.let {
        val source: LiveData<My_Result> = mainRepository.fetchApiresultFromClient(it.filter_search , it.filter_topics, it.filter_language , it.page_number)

        liveResult.addSource(source){ item->
            liveResult.value = item
            liveResult.removeSource(source)
        }

        source

        }
    }

}

Now I want to test that if searchQuery is set then:现在我想测试一下是否设置了 searchQuery:

  • switchMap is triggered switchMap 被触发
  • and when switchMap is triggered then mainRepository.fetchApiresultFromClient(..) is called当 switchMap 被触发时, mainRepository.fetchApiresultFromClient(..) 被调用
  • also if mainRepository.fetchApiresultFromClient(..) is called then the returned type is of LiveData如果 mainRepository.fetchApiresultFromClient(..) 被调用,则返回的类型是 LiveData

How do I do this?我该怎么做呢? All I have managed to do is :我所能做的就是:

@ExtendWith(InstantExecutorExtension::class)
class MainViewModelTest {

    lateinit var  mainViewModel: MainViewModel

    @Mock
     lateinit var  mainRepository: MainRepository


    @BeforeEach
    fun init()  {
    MockitoAnnotations.initMocks(this)
    mainViewModel = MainViewModel(mainRepository)
    }


    @Test
    @Throws(Exception::class)
    fun setSearchQuery_callsRepositoryMethod() {
    var filter_search: SearchQuery = SearchQuery("java", "","",1)

    }

}

To make your code easier to test you could replace it by:为了使您的代码更易于测试,您可以将其替换为:

class MainViewModel @Inject constructor(val mainRepository: MainRepository) : ViewModel() {

    val searchQuery:MutableLiveData<SearchQuery> = MutableLiveData()

    var apiData:LiveData<My_Result> = Transformations.switchMap(searchQuery) { query ->
        query?.let {
            mainRepository.fetchApiresultFromClient(it.filter_search , it.filter_topics, it.filter_language , it.page_number)
        }
    }
}

Then you should那你应该

  • mock mainRepository.fetchApiresultFromClient to return a MutableLiveData模拟mainRepository.fetchApiresultFromClient返回一个MutableLiveData
  • call mainViewModel.searchQuery.value = Query(...)调用mainViewModel.searchQuery.value = Query(...)
  • and assert that when mainRepository emits a new value apiData also emits the same value.并断言当mainRepository发出新值时apiData也发出相同的值。

If you have some test code to share I can help you to complete it如果您有一些测试代码要分享,我可以帮助您完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM