Skip to content

Instantly share code, notes, and snippets.

@SEAbdulbasit
Created September 24, 2022 17:01
Show Gist options
  • Save SEAbdulbasit/636c47715f87cb35137da39172213ed9 to your computer and use it in GitHub Desktop.
Save SEAbdulbasit/636c47715f87cb35137da39172213ed9 to your computer and use it in GitHub Desktop.
Music Details Unit test
class MusicDetailViewModelTest {
lateinit var SUT: MusicDetailViewModel
@MockK
val repo = mockk<MusicRepository>()
@MockK
val savedInstanceStateHandle = mockk<SavedStateHandle>(relaxed = true)
@Before
fun setUp() {
SUT = MusicDetailViewModel(
repository = repo,
savedStateHandle = savedInstanceStateHandle,
)
}
@Test
fun `give ViewModel initialise, when Music Id is passed, then Music Detail state should be emitted`(): Unit =
runTest {
coEvery {
savedInstanceStateHandle.getStateFlow(
any(),
1232
)
} returns emptyFlow<Int>().stateIn(this)
coEvery { repo.getMusic(1232) } returns flow {
emit(
MusicEntity(1232, "track name", "sf", "sfd", "sdf", "sdf")
)
}
SUT = MusicDetailViewModel(
repository = repo,
savedStateHandle = savedInstanceStateHandle
)
SUT.state.test {
val item = awaitItem()
assertEquals(true, item.uiMModel.musicTitle.isEmpty())
val item2 = awaitItem()
assertEquals(false, item2.uiMModel.previewUrl.isEmpty())
}
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment