Skip to content

測試

在某些測試中(例如遷移驗證),您可能希望將 Android 驅動程式替換為 JVM 驅動程式,這使您能夠測試涉及資料庫的程式碼,而無需 Android 模擬器或實體設備。為此,請使用 JVM SQLite 驅動程式:

kotlin
dependencies {
  testImplementation("app.cash.sqldelight:sqlite-driver:2.1.0")
}
groovy
dependencies {
  testImplementation "app.cash.sqldelight:sqlite-driver:2.1.0"
}
kotlin
// 當您的測試需要驅動程式時
@Before fun before() {
  driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
  Database.Schema.create(driver)
}

如果您使用的是隨 Android 綁定提供的 SQLite(而不是自行提供的 SQLite),您可以覆寫 sqlite-jdbc 的版本,使其符合您的 Android minSdkVersion,例如對於 API 23,請使用 SQLite 3.8.10.2:

groovy
dependencies {
  testImplementation('org.xerial:sqlite-jdbc') {
    // 覆寫 sqlite-driver 使用的 sqlite 版本以符合 Android API 23
    version { strictly('3.8.10.2') }
  }
}