Skip to content

Instantly share code, notes, and snippets.

@r4jiv007
Last active May 19, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r4jiv007/6080f9cc1984e23f1cf80a71981f3e98 to your computer and use it in GitHub Desktop.
Save r4jiv007/6080f9cc1984e23f1cf80a71981f3e98 to your computer and use it in GitHub Desktop.
@Test
fun test_polling_behaviour() {
Mockito.`when`(dataManager.getMatch(Mockito.anyInt())).thenReturn(
just(matchResponse))
Mockito.`when`(matchResponse.match()).thenReturn(mMatch)
val testScheduler = TestScheduler()
mSpyPresenter.setMatch(mMatch)
val testObserver = mSpyPresenter.getPollingObservable(MATCH_UPDATE_INTERVAL, testScheduler, testScheduler)
.subscribeOn(testScheduler)
.observeOn(testScheduler)
.test()
testObserver.assertNotCompleted()
.assertNoErrors()
.assertValueCount(0)
testScheduler.advanceTimeBy(0L, TimeUnit.SECONDS)
testObserver.assertValueCount(1)
testScheduler.advanceTimeBy(10L, TimeUnit.SECONDS)
testObserver.assertValueCount(2)
testScheduler.advanceTimeBy(10L, TimeUnit.SECONDS)
testObserver.assertValueCount(3)
}
@Test
fun test_polling_result_behaviour() {
Mockito.`when`(dataManager.getMatch(Mockito.anyInt())).thenReturn(
just(matchResponse))
Mockito.`when`(matchResponse.match()).thenReturn(mMatch)
val testScheduler = TestScheduler()
mSpyPresenter.setMatch(mMatch)
val spySubscriber: Subscriber<MatchResponse> = Mockito.spy(mSpyPresenter.pollSubscriber)
mSpyPresenter.getPollingObservable(MATCH_UPDATE_INTERVAL, testScheduler, testScheduler)
.subscribeOn(testScheduler)
.observeOn(testScheduler)
.subscribe(spySubscriber)
testScheduler.advanceTimeBy(0L, TimeUnit.SECONDS)
Mockito.verify(spySubscriber, Mockito.times(1)).onNext(matchResponse)
Mockito.verify(mSpyPresenter, Mockito.times(1)).updateScoreCard(mMatch)
testScheduler.advanceTimeBy(10, TimeUnit.SECONDS)
Mockito.verify(spySubscriber, Mockito.times(2)).onNext(matchResponse)
Mockito.verify(mSpyPresenter, Mockito.times(2)).updateScoreCard(mMatch)
testScheduler.advanceTimeBy(10, TimeUnit.SECONDS)
Mockito.verify(spySubscriber, Mockito.times(3)).onNext(matchResponse)
Mockito.verify(mSpyPresenter, Mockito.times(3)).updateScoreCard(mMatch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment