Skip to content

Instantly share code, notes, and snippets.

@kaszabimre
Created June 21, 2023 09:27
Show Gist options
  • Save kaszabimre/7309a1ca4c83f57ab48c27d5c5830fea to your computer and use it in GitHub Desktop.
Save kaszabimre/7309a1ca4c83f57ab48c27d5c5830fea to your computer and use it in GitHub Desktop.
Unit test for the searchUser method in the ViewModel class, utilizing the MockKMP for verification. The test ensures that when a mock username is provided, the method returns a non-empty list with the user. Mock objects and assertions are used to validate the expected behavior.
@Test
fun `given mock username when searchUser called then returns non-emptyList with the user`() = runTest {
// Given
val userName = MockData.userName
everySuspending { action.searchUser(isAny()) } returns Unit
every { store.getUsers() } returns flowOf(listOf(MockData.user))
every { store.isFetchingFinished() } returns flowOf(true)
// When
viewModel.searchUser(userName)
// Then
val result = viewModel.state.first()
assertEquals(userName, result.data.first().login)
verifyWithSuspend {
viewModel.searchUser(isAny())
viewModel.setState(isAny())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment