程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI??

开发过程中遇到Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?的问题如何解决?下面主要结合日常开发的经验,给出你关于Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?的解决方法建议,希望对你解决Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?有所启发或帮助;

目前我正在使用 Espresso 实现 UI 测试/仪器测试,以在加载状态和成功状态下测试 FragmentMovIE。由于多个测试库和其他东西,我的测试仪器必须包含刀柄注射。

UI 测试场景:

  1. 加载状态时,检查我的加载进度条并显示文本加载
  2. 当状态成功时,隐藏加载并显示回收视图
  3. 完成

问题

Loading state 在运行测试时被跳过。 @Test 将在 viewmodel postValue 成功状态后运行。所以,我无法在加载状态时进行测试。

当我不写 onVIEw(withID(R.ID.progressbar)).check(matches(isdisplayed())) 时,测试通过,但我想在加载状态时进行测试。

可能的修复,将 delay(10000) 添加到我的视图模型,这将延长加载状态的持续时间,但是,我的测试仍在等待成功状态。

目前我不使用 IDlingresource,因为那不能解决我的问题,

实际场景:

  1. 片段运行,
  2. viewmodel 获取数据,
  3. viewmodel 加载,
  4. viewmodel 成功,
  5. FragmentTest 运行,
  6. vIEwAssert 失败,

预期:

  1. 片段运行,
  2. viewmodel 获取数据,
  3. viewmodel 加载,
  4. FragmentTest 检查进度条,
  5. viewmodel 成功,
  6. FragmentTest 检查 recycleVIEw,
  7. 完成

为什么?以及如何?

代码

fragmentMovIE 的观察者


 private val viewmodel: MovIEviewmodel by viewmodels()

   [...]

 viewmodel.movIE.observe(vIEwlifecycleowner,{
            Log.d(tag,"viewmodelgetMovIE: ${it.status}")
            when (it.status) {
                Status.LOADING -> {
                    Toast.makeText(requireContext(),"loading..",Toast.LENGTH_SHORT).show()
                }
                Status.succesS -> {
                    binding.progressbar.visibility = VIEw.GONE
                    binding.txLoading.visibility = VIEw.GONE
                    if (it.data != null) {
                        List.addAll(it.data)
                        movIEAdapter.notifyDataSetChanged()
                    }
                }
                Status.ERROR -> {
                    binding.progressbar.visibility = VIEw.VISIBLE
                    binding.txLoading.text = it.message
                    Toast.makeText(requireContext(),"${it.messagE}",Toast.LENGTH_SHORT).show()
                }
            }
        })

视图模型

@Hiltviewmodel
class MovIEviewmodel @Inject constructor(
    private val repository: MovIERepository,private val dispatcher: CoroutInedispatcher
) : viewmodel() {

    // new code
    private val movIEPost = mutablelivedata<resourceHelper<ArrayList<MovIEData>>>()

    val movIE: liveData<resourceHelper<ArrayList<MovIEData>>>
        get() = movIEPost

    init {
        fetchMovIE()
    }

    private fun fetchMovIE() {
        viewmodelScope.launch(dispatcher) {
            movIEPost.postValue(resourceHelper.loading(null))
            repository.getMovIE(1).let { movIE ->
                movIEPost.postValue(movIE)
            }
        }
    }


}

电影片段测试

package com.unlink.movIEcatalogue6.ui.movIE

import androIDx.test.espresso.Espresso.onVIEw
import androIDx.test.espresso.assertion.VIEwAssertions.matches
import androIDx.test.espresso.matcher.VIEwMatchers.isdisplayed
import androIDx.test.espresso.matcher.VIEwMatchers.withID
import androIDx.test.ext.junit.rules.ActivityScenarioRule
import androIDx.test.ext.junit.runners.AndroIDJUnit4
import androIDx.test.filters.LargeTest
import com.unlink.movIEcatalogue6.MainActivity
import com.unlink.movIEcatalogue6.R
import dagger.hilt.androID.tesTing.HiltAndroIDRule
import dagger.hilt.androID.tesTing.HiltAndroIDTest
import org.junit.After
import org.junit.before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@HiltAndroIDTest
@RunWith(AndroIDJUnit4::class)
@LargeTest
class MovIEFragmentTest {
    @get:rule(order = 0)
    var hiltRule = HiltAndroIDRule(this)

    @get:rule(order = 1)
    var activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Before
    fun setUp() {
        hiltRule.inject()
    }

    @After
    fun tearDown() {

    }

    @Test
    fun happyPath() {
        onVIEw(withID(R.ID.progressbar)).check(matches(isdisplayed())) // error
        // how can i test on loading?
        onVIEw(withID(R.ID.recycleMovIE)).check(matches(isdisplayed()))
    }
}

错误

androIDx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the SELEcted vIEw.
Expected: is displayed on the screen to the user
Got: "Progressbar{ID=2131231016,res-name=progressbar,visibility=GONE,wIDth=1080,height=44,has-focus=false,has-focusable=false,has-window-focus=true,is-clickable=false,is-enabled=true,is-focused=false,is-focusable=false,is-layout-requested=true,is-SELEcted=false,layout-params=androIDx.consTraintlayout.Widget.ConsTraintLayout$LayoutParams@a455ee2,tag=null,root-is-layout-requested=false,has-input-connection=false,x=0.0,y=0.0}"

显示错误是因为我的进度条消失了。因为我的测试在成功状态后检查进度条。

屏幕截图

加载状态,但我的测试没有运行@Test

loading state

成功状态,然后@Test开始

success state

导致错误视图消失

error

任何回应将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?全部内容,希望文章能够帮你解决Espresso UI 测试,无法检查加载状态,如何使用实时数据和视图模型测试 UI?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。