Created
January 20, 2020 21:05
-
-
Save CostaFot/517b299dba04dd420cabd9627089aa44 to your computer and use it in GitHub Desktop.
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 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