Skip to content

Instantly share code, notes, and snippets.

@diefferson
Created March 13, 2020 15:18
Show Gist options
  • Save diefferson/7b2ae13df624bd627da549e00e882e52 to your computer and use it in GitHub Desktop.
Save diefferson/7b2ae13df624bd627da549e00e882e52 to your computer and use it in GitHub Desktop.
Clean architecture viewModel test
@ExperimentalCoroutinesApi
class RatingViewModelTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var viewModel: RatingViewModel
private val setRatedCase: SetRated = mock()
private val testDispatcher = TestCoroutineDispatcher()
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
viewModel = spy(RatingViewModel(setRatedCase))
}
@After
fun cleanUp() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
@Test
fun `set rate should call method`() = runBlocking {
val result = ResultAsync.with(this, suspend { Unit })
whenever(setRatedCase.invoke(any())).doReturn(result)
viewModel.setRated()
verify(setRatedCase).invoke(any())
assertEquals(viewModel.ratedSuccess.await(), true)
}
}
suspend fun <T> LiveData<T>.await(): T {
return suspendCoroutine{ cont ->
this.observeForever {
cont.resume(it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment