Skip to content

Instantly share code, notes, and snippets.

@CostaFot
Created January 20, 2020 21:05
Show Gist options
  • Save CostaFot/517b299dba04dd420cabd9627089aa44 to your computer and use it in GitHub Desktop.
Save CostaFot/517b299dba04dd420cabd9627089aa44 to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
class ViewModelTest {
@get:Rule
var coroutinesTestRule = TestCoroutineDispatcherRule()
@get:Rule
var rule: TestRule = InstantTaskExecutorRule()
private val stateObserver: Observer<State> = mock()
private lateinit var lifeCycleTestOwner: LifeCycleTestOwner
private lateinit var testViewModel: TestViewModel
@Before
fun setUp() {
lifeCycleTestOwner = LifeCycleTestOwner()
lifeCycleTestOwner.onCreate()
testViewModel = TestViewModel()
testViewModel.pollStateData.observe(lifeCycleTestOwner, stateObserver)
}
@After
fun tearDown() {
lifeCycleTestOwner.onDestroy()
}
@Test
fun `when polling starts the observer is notified the correct amount of times depending on delay`() {
// Given
lifeCycleTestOwner.onResume()
// When
runBlockingTest {
testViewModel.fetchSomething()
// Then
Verify on stateObserver that pollStateObserver.onChanged(State.Loading) was called
Verify on stateObserver that pollStateObserver.onChanged(State.Post) was called
VerifyNoFurtherInteractions on pollStateObserver
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment