android - 在测试 Room AutoMigrations 时,我应该将什么传递给 runMigrationsAndValidate?

标签 android android-sqlite android-room android-testing

使用 Room AutoMigrations 时, Migration本身是自动生成的。但是为了unit test the migration ,我必须通过 Migration反对runMigrationsAndValidate .我应该在这里通过什么?

@RunWith(AndroidJUnit4::class)
class MigrationTest {
    private val TEST_DB = "migration-test"

    @Rule
    val helper: MigrationTestHelper = MigrationTestHelper(
            InstrumentationRegistry.getInstrumentation(),
            MigrationDb::class.java.canonicalName,
            FrameworkSQLiteOpenHelperFactory()
    )

    @Test
    @Throws(IOException::class)
    fun migrate1To2() {
        var db = helper.createDatabase(TEST_DB, 1).apply {
            // db has schema version 1. insert some data using SQL queries.
            execSQL(...)

            // Prepare for the next version.
            close()
        }

        // Re-open the database with version 2 and provide
        // Migration as the migration process.
        db = helper.runMigrationsAndValidate(TEST_DB, 2, true, /* WHAT TO PASS HERE? */)
    }
}

最佳答案

据我了解,在新的 AutoMigration 中,我们不需要自己处理迁移。
所以首先,更新你的 MigrationTestHelper到具有 AutoMigration 支持的 API:

val helper = MigrationTestHelper(
    InstrumentationRegistry.getInstrumentation(),
    Database::class.java,
    ArrayList()
)
对于空的 ArrayList,请参见:https://github.com/androidx/androidx/blob/6782be17cf8b3077a6aa6b342c64b7acff22f865/room/room-testing/src/main/java/androidx/room/testing/MigrationTestHelper.java#L339
然后从调用中删除迁移:db = helper.runMigrationsAndValidate(TEST_DB, 2, true)

关于android - 在测试 Room AutoMigrations 时,我应该将什么传递给 runMigrationsAndValidate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69026958/

相关文章:

android - 在 WebView 中显示图像

android - SeekBar 拇指(9 补丁)无法识别

android - 自动升级数据库版本

android - 如果存在则更新记录,否则在 Room 中插入新记录

Android Room notNull 字段需要 null 默认值

android - 如何根据屏幕位置而不是 map 位置定位 InfoWindows?

android - 带 xml 图标的 DrawableLeft

android - AsyncTaskLoader vs AsyncTask 用于在编辑单个实体 Activity 中加载单个实体?

android - Sqlite 数据库中的 ID 冲突?

android - 如何使用 RxJava 从 Android Room 数据库选择查询中获取返回值