Daw*_*yży 8 android datastore android-uiautomator dagger-hilt
我刚刚将 DataStore 添加到我们的代码库中。之后,我发现所有顺序 UI 测试都失败 - 测试用例中的第一个测试通过,但下一个失败并显示There are multiple DataStores active for the same file.
我使用 Hilt 提供一个数据存储实例
\n@InstallIn(SingletonComponent::class)\n@Module\ninternal object DataStoreModule {\n\n @Singleton\n @Provides\n internal fun provideConfigurationDataStore(\n @ApplicationContext context: Context,\n configurationLocalSerializer: ClientConfigurationLocalSerializer\n ): DataStore<ClientConfigurationLocal> = DataStoreFactory.create(\n serializer = configurationLocalSerializer,\n produceFile = { context.dataStoreFile("configuration.pb") }\n )\n}\nRun Code Online (Sandbox Code Playgroud)\n我猜这是因为In a Hilt test, the singleton component\xe2\x80\x99s lifetime is scoped to the lifetime of a test case rather than the lifetime of the Application.
关于如何解决这个问题有什么想法吗?
\n我遇到过同样的问题。我尝试过但不起作用(正确)的一种解决方案是确保测试完成后,删除数据存储文件(整个文件夹)并关闭范围(您在“管理器”类中管理的覆盖范围) ,像这样: https: //github.com/wwt/testing-android-datastore/blob/main/app/src/androidTest/java/com/wwt/sharedprefs/DataStoreTest.kt
我将其放在用于这些 UI 测试finished()的块中。TestWatcher由于某种原因,这还不够,所以我最终没有深入研究原因。
相反,我只是使用了一个更简单的解决方案:UI 测试将使用自己的 Dagger 组件,该组件有自己的StorageModule模块,提供自己的IStorage实现,对于 UI 测试来说,仅由内存中的映射支持,而在生产 Dagger 模块上将通过数据存储备份它:
interface IStorage {
suspend fun retrieve(key: String): String?
suspend fun store(key: String, data: String)
suspend fun remove(key: String)
suspend fun clear()
Run Code Online (Sandbox Code Playgroud)
在我的例子中,我更喜欢这种方法,因为我不需要在 UI 测试中测试该存储的实际磁盘持久性,但如果我需要它,我会进一步研究如何可靠地确保数据存储文件夹和范围每次 UI 测试之前/之后进行清理。
| 归档时间: |
|
| 查看次数: |
3048 次 |
| 最近记录: |