我正在尝试从LiveData迁移到Room Dao中的Flow。应用程序运行正常,但测试行为存在问题。当我运行测试时,它会不确定地启动和运行。 我也尝试过使用 kotlinx.coroutines.test runBlockingTest ,但是我遇到了诸如“ here”这样的“此工作尚未完成”的问题。有人可以指出正确的方向如何测试CoresDao的行为吗?
@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))
}
}
}
答案 0 :(得分:3)
要测试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
答案 1 :(得分:2)
由于您已经在使用TestCoroutineDispatcher
,因此在您的示例中使用runBlockingTest
不会做任何事情。
收集后,您必须cancel
Flow
或启动scope
Flow
编辑:可以找到此类规则的示例here
答案 2 :(得分:0)
事实证明我没有正确处理Flow的收集和取消,这可能是造成问题的原因。下面是有效的代码。 可以找到更复杂的示例here。
@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()
}