Luke
11/26/2024, 2:48 PMinterface MyRepository {
val state: StateFlow<State>
sealed interface State {
data class Foo(...): State
data class Bar(...): State
}
}
Now, I need to run some code each time the state changes while the application runs. What is the best way to set a collector on the state and test that the collector works properly?ephemient
11/27/2024, 4:35 AMclass MyApplication : Application() {
val mainScope = MainScope()
override fun onCreate() {
mainScope.launch {
myRepository.state.collect { ... }
}
}
override fun onClose() {
mainScope.cancel()
}
}
class MyTest {
@Before
fun setUp() {
Dispatchers.setMain(StandardTestDispatcher())
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun testMyApplication() = runTest { ... }
}
work or is the application initialized too early in test?Luke
11/27/2024, 1:49 PMJavier
11/27/2024, 1:53 PMturbine
library from Cash organization on GitHub