android - Android - 通过使用 launchFragmentInContainer 使用 Espresso 测试片段永远不会完成
问题描述
我的测试从未完成,我完全不知道为什么。我可以看到手机屏幕上显示的吐司。日志中绝对没有任何内容。
@RunWith(AndroidJUnit4::class)
@SmallTest
class BaseDataFragmentUITest
{
@Test
fun isDisplayingToastWhenFAILED_TO_UPDATE()
{
val fragmentScenario = launchFragmentInContainer<TestBaseDataFragmentImp>()
val toastString: String = context.resources.getString(com.developerkurt.gamedatabase.R.string.data_update_fail)
fragmentScenario.onFragment {
it.handleDataStateChange(BaseRepository.DataState.FAILED_TO_UPDATE)
onView(withText(toastString)).inRoot(withDecorView(not(it.requireActivity().getWindow().getDecorView()))).check(matches(isDisplayed()))
}
}
}
解决方案
显然,不应在 onFragment 块内进行 Espresso 断言。因此,当我这样编写测试时,它起作用了:
@Test
fun isDisplayingToastWhenFAILED_TO_UPDATE()
{
val fragmentScenario = launchFragmentInContainer<TestBaseDataFragmentImp>()
val toastString: String = context.resources.getString(com.developerkurt.gamedatabase.R.string.data_update_fail)
var decorView: View? = null
fragmentScenario.onFragment {
it.handleDataStateChange(BaseRepository.DataState.FAILED_TO_UPDATE)
decorView = it.requireActivity().getWindow().getDecorView()
}
onView(withText(toastString)).inRoot(withDecorView(not(decorView!!))).check(matches(isDisplayed()))
}
推荐阅读
- visual-studio - 我的 Visual Studio 安装程序项目中这些随机命名的 DLL 是什么?
- firebase - Firebase 重定向到相同的路径名
- python-2.7 - 定义 NAOqi ALProxy 代理
- javascript - 智能合约在 remix 上正常工作,但在 web3 上不能正常工作
- python - 如何复制 pyre2.Pattern 对象
- sql - 使用proc sql将具有不同结构的多个表堆叠到一个表中
- javascript - Google Material Design - 禁用开关
- java - 无法反序列化具有 int 的 String 类型的值
- jenkins - 根据 Jenkins 中的另一个构建触发定期构建
- regex - 在一个命令行中以有序的方式在 Notepad++ 中执行一些不同的命令