用Kotlin推论和Flow测试房间DAO的方法



我正试图在我的Room Dao中从LiveData迁移到Flow。应用程序运行良好,但我在测试行为方面有问题。当我进行测试时,它开始并不知疲倦地运行。我还尝试使用kotlin.coroutines.test-runBlockingTest,但我遇到了像这里这样的"此作业尚未完成"的问题。有人能为我指明正确的方向吗?如何测试我的核心道的行为?

@Dao
interface CoresDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCores(cores: List<Core>)
@Transaction
suspend fun replaceCoresData(cores: List<Core>) {
deleteAllCores()
insertCores(cores)
}
@Query("SELECT * FROM cores_table")
fun getAllCores(): Flow<List<Core>>
@Query("DELETE FROM cores_table")
suspend fun deleteAllCores()
}
@RunWith(AndroidJUnit4::class)
class CoresDaoTest {
private lateinit var database: SpaceDatabase
private lateinit var coresDao: CoresDao
private val testDispatcher = TestCoroutineDispatcher()
private val testCoresList = listOf(core2, core3, core1)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room.inMemoryDatabaseBuilder(context, SpaceDatabase::class.java).build()
coresDao = database.coresDao()
}
@After
fun cleanup() {
database.close()
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
@Test
fun testGetAllCores(): Unit = runBlocking {
withContext(Dispatchers.Main) {
runBlocking { coresDao.insertCores(testCoresList) }
val coresList = mutableListOf<Core>()
coresDao.getAllCores().collect { cores -> coresList.addAll(cores) }
assertThat(coresList.size, equalTo(testCoresList.size))
}
}
}

为了测试Flow,我发现最好的API是.take(n).toList()。您可以使用runBlockingTest,而不需要使用withContext将执行转移到另一个线程。

你可以在这里找到一个工作原理的例子:https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/test/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/NeverEndingFibonacciProducerTest.kt#L38

由于您已经在使用TestCoroutineDispatcher,因此在您的示例中使用runBlockingTest不会有任何作用。。您必须cancel收集后的Flow或启动Flowscope

编辑:可以在这里找到这样一个规则的例子

事实证明,我没有正确处理Flow收集和取消,这可能是问题的原因。以下是有效的代码。更复杂的例子可以在这里找到。

@RunWith(AndroidJUnit4::class)
class CoresDaoTest {
private lateinit var database: SpaceDatabase
private lateinit var coresDao: CoresDao
private val testDispatcher = TestCoroutineDispatcher()
private val testCoresList = listOf(core2, core3, core1)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room
.inMemoryDatabaseBuilder(context, SpaceDatabase::class.java)
.setTransactionExecutor(Executors.newSingleThreadExecutor())
.build()
coresDao = database.coresDao()
}
@After
fun cleanup() {
database.close()
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
@Test
fun testInsertAndGetAllCores() = runBlocking {
coresDao.insertCores(testCoresList)
val latch = CountDownLatch(1)
val job = launch(Dispatchers.IO) {
coresDao.getAllCores().collect { cores ->
assertThat(cores.size, equalTo(testCoresList.size))
latch.countDown()
}
}
latch.await()
job.cancel()
}

最新更新