Created
March 13, 2020 15:18
-
-
Save diefferson/7b2ae13df624bd627da549e00e882e52 to your computer and use it in GitHub Desktop.
Clean architecture viewModel test
This file contains hidden or 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
@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