Slide 17
Slide 17 text
// ...
// @get:Rule
// var testCoroutinesRule = TestCoroutinesRule()
private lateinit var mainViewModel: MainViewModel
@MockK
private lateinit var itemRepository: ItemRepository
@Before
fun setUp() {
MockKAnnotations.init(this, relaxed = true)
// mainViewModel = MainViewModel(itemRepository)
}
@Test
fun `MainViewModel#loadItemList()`() {
// Given
val expectItemList = listOf(Item(1, "item1"), Item(2, "item2"))
coEvery { itemRepository.getItemList() } answers { expectItemList }
val observer = mockk>>>(relaxUnitFun = true)
runBlocking {
mainViewModel = MainViewModel(itemRepository, this)
mainViewModel.itemListStatus.observeForever(observer)
// When
mainViewModel.loadItemList()
}
// Then
coVerify(exactly = 1) { itemRepository.getItemList() }
verifySequence {
observer.onChanged(Status.Loading)
observer.onChanged(Status.Success(expectItemList))
}
}
MainViewModelTest
← runBlockingͷείʔϓΛViewModel
͢