If this is not the right place to ask this, please...
# android-architecture
l
If this is not the right place to ask this, please direct me to the proper channel I have an implementation of an interface that looks like this:
Copy code
interface 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?
e
does the straightforward
Copy code
class MyApplication : Application() {
    val mainScope = MainScope()
    override fun onCreate() {
        mainScope.launch {
            myRepository.state.collect { ... }
        }
    }
    override fun onClose() {
        mainScope.cancel()
    }
}
Copy code
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?
l
I would like to test with unit tests since I'm working on a library. We do have a sample app with UI tests but since no Android framework is involved in this logic, I think unit tests would be better
j
If you want to test any kind of flow, check the
turbine
library from Cash organization on GitHub
1