问题描述
我正在学习Espresso的UI测试。我想测试回收站视图到底部的滚动,然后才从视图模型加载下一页并将其传递给回收站视图。
我的片段中有以下onScrollListener:
private fun setupOnScrollListener() {
recyclerViewApi.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView,newState: Int) {
super.onScrollStateChanged(recyclerView,newState)
val isRecyclerViewBottom = !recyclerView.canScrollVertically(1) &&
newState == RecyclerView.SCROLL_STATE_IDLE
if (isRecyclerViewBottom) {
downloadNextPage()
}
}
})
}
private fun downloadNextPage() {
showProgressBar(true)
viewModel.getNextMovies()
}
当我使用Log.d()
进行手动测试时,效果很好。
我的问题:如何使用Espresso(如果您比Espresso知道的更多,则可以使用其他API)将回收站视图滚动到此状态:
isRecyclerViewBottom = !recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE
,
因此我的downloadNextPage()
将被调用,而测试功能将提取更多数据。
我的测试功能:
@Test
fun scrollToBottom_isNextPageLoaded(){
every { repository.getApiMovies(any(),any()) } returns
Flowable.just(Resource.success(moviesList1_5)) andThen
Flowable.just(Resource.success(moviesList1_10))
val scenario = launchFragmentInContainer<ApiFragment>(factory = fragmentsFactory)
//first 5 items are in view,so I go to the last item (index 4)
recyclerView.perform(scrollToPosition<ViewHolder>(4))
recyclerView.perform(swipeDown())
//Below doesn't make any difference
Thread.sleep(1000L)
verify(exactly = 2) { repo.getApiMovies(any(),any()) }
}
我使用Robolectric,Mockk,Espresso。我在这里嘲笑了存储库类,该类传递给ViewModelFactory的构造函数,该模型传递给ApiFragment的构造函数。
来自JUnit的消息
java.lang.AssertionError: Verification failed: call 1 of 1: ApiRepository(repo#4).getApiMovies(any(),any())).
One matching call found,but needs at least 2 and at most 2 calls
Call: ApiRepository(repo#4).getApiMovies(Top Rated,1)
这不是我的第一个测试功能。其他一切都很好。我只是不知道如何使Espresso转到回收器视图的底部并“拉起”其底部边缘以调用downloadNextPage()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)