Skip to content

Instantly share code, notes, and snippets.

@melihaksoy
Created July 12, 2019 15:08
Show Gist options
  • Save melihaksoy/9c0d36feaeb07f7bc4c85c6fca293835 to your computer and use it in GitHub Desktop.
Save melihaksoy/9c0d36feaeb07f7bc4c85c6fca293835 to your computer and use it in GitHub Desktop.
Case
abstract class TestBaseViewModel : ViewModel() {
abstract suspend fun loadData()
init {
viewModelScope.launch {
loadData()
}
}
}
class TestViewModel : TestBaseViewModel() {
override suspend fun loadData() {
println("Boop")
}
}
class BaseTest {
@ExperimentalCoroutinesApi
private val dispatcher = TestCoroutineDispatcher()
@BeforeEach
@ExperimentalCoroutinesApi
fun setUp() {
Dispatchers.setMain(dispatcher)
}
@AfterEach
@ExperimentalCoroutinesApi
fun tearDown() {
Dispatchers.resetMain()
dispatcher.cleanupTestCoroutines()
}
@Test
fun `init should invoke loadData`() {
val vm = spyk(TestViewModel())
coVerify(exactly = 1) {
vm.loadData()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment