我正在尝试在我的 RecyclerView 上运行单元测试。对于我的第一个测试,我想看看是否显示了 RecyclerView。
@RunWith(RobolectricTestRunner::class)
class WordListFragmentTest {
// Executes task sin the Architecture component in the same thread.
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var scenario: FragmentScenario<WordListFragment>
private lateinit var viewModel: MainViewModel
val word = Word("Word")
@Before
fun setup() {
viewModel = mock(MainViewModel::class.java)
scenario = launchFragment(
factory = MainFragmentFactory(viewModel),
fragmentArgs = null,
themeResId = R.style.Theme_Words,
initialState = Lifecycle.State.RESUMED
)
}
@Test
fun `recyclerView displayed`() {
onView(withId(R.id.recyclerView))
.check(matches(isDisplayed()))
}
运行测试后,我收到以下错误。
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
这似乎与在片段中提交列表的 LiveData 观察者有关。如果我注释掉提交功能,测试将运行。
片段。
class WordListFragment(private val viewModel: MainViewModel) : Fragment() {
...
private fun submitList() {
viewModel.wordList.observe(viewLifecycleOwner, {
it?.let {
rvAdapter.submitList(it)
}
})
}
}
面视图模型
class MainViewModel @Inject constructor(
var repository: IWordRepository,
@IoDispatcher var ioDispatcher: CoroutineDispatcher
) : ViewModel() {
var wordList: LiveData<List<Word>> = repository.allWords
...
}
此链接表明 Robolectric 将默认为 LooperMode.LEGACY 行为,但这可以通过将 @LooperMode(NewMode) 注释应用于测试包、测试类或测试方法或通过“robolectric.looperMode”系统属性来覆盖。运行测试时,我仍然遇到同样的错误。