Created
September 24, 2022 17:01
-
-
Save SEAbdulbasit/636c47715f87cb35137da39172213ed9 to your computer and use it in GitHub Desktop.
Music Details Unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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