I have a class that looks something like
class Foo {
private val scope = Job() + Dispatchers.IO
val emissions = PublishSubject.create<Bar>()
fun doSomething() {
scope.launch {
// Do a whole bunch of work...
withContext(Dispatchers.Main) emissions.onNext(bar)
}
}
}
And I'm trying to come up with a way to unit test it. I've tried making the scope injectable and writing something like
@Test fun testFoo() = runBlockingTest {
Dispatchers.setMain(TestCoroutineDispatcher())
val foo = Foo(this)
foo.doSomething()
foo.emissions.assertStuff()
}
but that doesn't seem to work. The assertion happens before the coroutine inside doSomething() has finished.
I've also tried making this dispatchers injectable, providing Dispatchers.Unconfined, but that didn't help either. Is there something wrong with this approach?
PublishSubject?Subject,Relay,` Channel` or something similar