使用 Rx 测试房间插入时测试失败
Tests fail when testing Room insertion with Rx
我正在尝试对我的单个实体进行简单的 CRUD 测试,但我无法弄清楚我做错了什么,因此测试总是失败 AssertionError
。
这是我的数据库设置:
@Database(entities = [Habit::class], version = 1)
@TypeConverters(Converter::class)
abstract class CareFlectDatabase : RoomDatabase() {
abstract fun habitDao(): HabitDao
}
实体:
@Entity(tableName = "user_habits")
data class Habit(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Long?,
@ColumnInfo(name = "habit_title")
val habitTitle: String?,
@ColumnInfo(name = "start_date")
val startDate: Date?,
@ColumnInfo(name = "end_date")
val endDate: Date?,
@ColumnInfo(name = "receive_notification")
val receiveNotification: Boolean?
)
现在是测试:
@Test
fun insertHabitTest() {
//dependencies correctly instatiated
//this line does complete
habitDao.insertHabit(habit)
habitDao.selectAll().test().assertValue { list ->
//this line fails
list.isNotEmpty()
}
}
我的道查询:
@Query("SELECT * FROM user_habits")
fun selectAll(): Flowable<List<Habit>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertHabit(habit: Habit): Completable
如果我从依赖项中遗漏了什么,请看一下:
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
//tests
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:core:1.2.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.28.2'
试试
@Before
fun before() {
RxAndroidPlugins.reset()
RxJavaPlugins.reset()
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler {
Schedulers.trampoline()
}
}
@Test
fun insertHabitTest() {
//dependencies correctly instatiated
//this line does complete
habitDao.insertHabit(habit).subscribeOn(Schedulers.io()).subscribe()
val observer = habitDao.selectAll().subscribeOn(Schedulers.io()).test()
observer.awaitTerminalEvent()
observer.assertNoErrors().assertValue { list ->
//this line fails
list.isNotEmpty()
}}
已解决:我唯一缺少的是 blockingAwait()
调用,当我的 INSERT
查询发生并且测试通过时。
所以代替:
habitDao.insertHabit(habit)
应该是:
habitDao.insertHabit(habit).blockingAwait()
更多参考 here.
我正在尝试对我的单个实体进行简单的 CRUD 测试,但我无法弄清楚我做错了什么,因此测试总是失败 AssertionError
。
这是我的数据库设置:
@Database(entities = [Habit::class], version = 1)
@TypeConverters(Converter::class)
abstract class CareFlectDatabase : RoomDatabase() {
abstract fun habitDao(): HabitDao
}
实体:
@Entity(tableName = "user_habits")
data class Habit(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Long?,
@ColumnInfo(name = "habit_title")
val habitTitle: String?,
@ColumnInfo(name = "start_date")
val startDate: Date?,
@ColumnInfo(name = "end_date")
val endDate: Date?,
@ColumnInfo(name = "receive_notification")
val receiveNotification: Boolean?
)
现在是测试:
@Test
fun insertHabitTest() {
//dependencies correctly instatiated
//this line does complete
habitDao.insertHabit(habit)
habitDao.selectAll().test().assertValue { list ->
//this line fails
list.isNotEmpty()
}
}
我的道查询:
@Query("SELECT * FROM user_habits")
fun selectAll(): Flowable<List<Habit>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertHabit(habit: Habit): Completable
如果我从依赖项中遗漏了什么,请看一下:
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
//tests
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:core:1.2.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.28.2'
试试
@Before
fun before() {
RxAndroidPlugins.reset()
RxJavaPlugins.reset()
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler {
Schedulers.trampoline()
}
}
@Test
fun insertHabitTest() {
//dependencies correctly instatiated
//this line does complete
habitDao.insertHabit(habit).subscribeOn(Schedulers.io()).subscribe()
val observer = habitDao.selectAll().subscribeOn(Schedulers.io()).test()
observer.awaitTerminalEvent()
observer.assertNoErrors().assertValue { list ->
//this line fails
list.isNotEmpty()
}}
已解决:我唯一缺少的是 blockingAwait()
调用,当我的 INSERT
查询发生并且测试通过时。
所以代替:
habitDao.insertHabit(habit)
应该是:
habitDao.insertHabit(habit).blockingAwait()
更多参考 here.