val Context.dataStore by preferencesDataStore(Constants.PREFERENCES_NAME)
@Singleton
class DataStoreManager @Inject constructor(@ApplicationContext appContext: Context) {
private val settingsDataStore: DataStore<Preferences> = appContext.dataStore
private val androidIndexVersion = intPreferencesKey(Constants.ANDROID_INDEX_VERSION)
suspend fun setXyzKey(mode: Int) {
settingsDataStore.edit { settings ->
settings[XyzKey] = mode
}
}
val getXyzKey: Flow<Int> = settingsDataStore.data.map { preferences ->
preferences[XyzKey] ?: -1
}
}
I try to find out the solution which work in Android unit test not to run in instrumentation test.
You want to test a component from android, which is DataStore
, but since it is part of the android stuff, you have to run your tests in an emulator, which means you must use Instrumented Tests.
You can on the other hand, create an interface type for the things you want to store and have an implementation with DataStore
so that you can pass later on a Fake implementation of that interface to other dependents in your Normal Unit Tests.