I am testing single use-case. but test observer will not emit any thing in test environment.
- even if i change subscribeOn() from Schedulers.newThread() to TestScheduler() still test fail. i am confuse. and i don't know what i am doing wrong here?
- if i remove subscribeOn,observeOn method then test pass successfully.then what is the actual use of TestScheduler() ?
following are the UseCase.kt file
package com.*.domain.interactors
import com.*.domain.executors.PostExecutionThread
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
/**
* *
*
* Created by mcd on 3/11/2018.
*/
abstract class UseCase<T>(val postExecutionThread: PostExecutionThread) {
abstract fun buildUseCaseBuilder(): Observable<T>
/**
* execute method for observables
*/
open fun execute(): Observable<T> {
return buildUseCaseBuilder()
.subscribeOn(Schedulers.newThread())
.observeOn(postExecutionThread.getScheduler())
}
}
following the test class with above class under test
package com.*.domain
import com.*.domain.executors.PostExecutionThread
import com.*.domain.interactors.UseCase
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import io.reactivex.observers.TestObserver
import io.reactivex.schedulers.TestScheduler
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.`when` as _when
@Suppress("RemoveRedundantBackticks")
/**
* *
*
* Created by mcd on 3/19/2018.
*/
class UseCaseTest {
val postExecutionThread=Mockito.mock(PostExecutionThread::class.java)
val result: String="test"
val testObserver=TestObserver<String>()
@Before
fun setUp() {
_when(postExecutionThread.getScheduler()).thenReturn(TestScheduler())
}
@Test
fun `test execute method of use case`() {
println(" thread type ${postExecutionThread.getScheduler()}")
//test fail
//java.lang.AssertionError: Value count differs; Expected: 1 [test], Actual: 0 [] (latch = 1, values = 0, errors = 0, completions = 0)
TestUseCase(postExecutionThread).execute().test()
.assertResult(result)
//subscriber not print any thing
TestUseCase(postExecutionThread).execute().subscribe{
println("called $it")
}
}
inner class TestUseCase(postExecutionThread: PostExecutionThread?) : UseCase<String>(postExecutionThread!!) {
override fun buildUseCaseBuilder(): Observable<String> {
return Observable.just(result)
}
}
}
Aucun commentaire:
Enregistrer un commentaire