Abhimanyu
09/26/2024, 5:16 PMNo value produced in 3s
Details in thread 🧵 .Abhimanyu
09/26/2024, 5:17 PMclass UsecaseTest {
private val testDispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(testDispatcher)
private lateinit var SUT: Usecase
@Before
fun setUp() {
SUT = Usecaseimpl(
coroutineScope = testScope,
)
}
@Test
fun refresh() = testScope.runTest {
turbineScope {
val receiver = SUT.refreshSignal.testIn(
scope = testScope,
)
SUT.refresh()
assertEquals(
Unit,
receiver.awaitItem(),
)
receiver.awaitComplete()
}
}
}
SUT code
class UsecaseImpl(
private val coroutineScope: CoroutineScope,
): Usecase {
override val refreshSignal: MutableSharedFlow<Unit> = MutableSharedFlow(
replay = 0,
extraBufferCapacity = 1,
)
override fun refresh() {
refreshSignal.tryEmit(Unit)
}
}
Abhimanyu
09/26/2024, 5:22 PMreceiver.cancel()
receiver.awaitComplete()
receiver.awaitError()
And also by replacing testScope
with backgroundScope
.
> testIn
cannot automatically clean up its coroutine, so it is up to you to ensure that the running flow terminates. Use `runTest`'s backgroundScope
, and it will take care of this automatically. Otherwise, make sure to call one of the following methods before the end of your scope:
> • cancel()
> • awaitComplete()
> • awaitError()
> Otherwise, your test will hang.Abhimanyu
09/26/2024, 5:31 PMSUT.refreshSignal.test {
SUT.refresh()
assertEquals(
Unit,
awaitItem(),
)
}
I am not able to make it work with turbineScope
.
Can anyone please help share what change is required for turbineScope
thank you color?Daniel Weidensdörfer
09/27/2024, 9:20 AMDaniel Weidensdörfer
09/27/2024, 9:22 AMfun test() = runTest(YourTestDispatcher()) {
val tested = YourUseCase(scope = backgroundScope)
tested.flow.test {
awaitItem()
...
}
}
Abhimanyu
09/27/2024, 11:55 AMAbhimanyu
09/27/2024, 11:57 AMDaniel Weidensdörfer
09/27/2024, 12:04 PM.test
is the turbine lambda.Abhimanyu
09/27/2024, 12:11 PMDaniel Weidensdörfer
09/27/2024, 12:16 PM