使用 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/