【问题标题】:Tests fail when testing Room insertion with Rx使用 Rx 测试房间插入时测试失败
【发布时间】:2019-09-30 12:55:14
【问题描述】:

我正在尝试对我的单个实体进行简单的 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'

【问题讨论】:

    标签: android rx-java2 android-room


    【解决方案1】:

    已解决:当我的 INSERT 查询发生并且测试通过时,我唯一缺少的是 blockingAwait() 调用。

    所以而不是:

    habitDao.insertHabit(habit)
    

    应该是:

    habitDao.insertHabit(habit).blockingAwait()
    

    更多参考here.

    【讨论】:

      【解决方案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()
      }}
      

      【讨论】:

      • 需要永远执行
      • 更新的建议看看
      • anw ,仍然需要永远
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多