I'm writing methods to test my Room DAO:
@Dao
interface SimpleStationDao {
@Query("SELECT * FROM simple_stations")
suspend fun getAllStations(): List<SimpleStation>
@Query("SELECT * FROM simple_stations WHERE is_favorite = 1")
suspend fun getFavorites(): List<SimpleStation>
}
In my test class I have two stations - ordinary and favorite - and also a list of them. There are two test methods - to get all stations and only favorite ones. The problem is that getFavorites()
Test method always asserts true(for example, when I change if condition to if (!item.isFavorite)
). What is wrong with it?
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class SimpleStationDaoTest : CacheDatabaseTest() {
private var ordinaryStation = SimpleStation(
id = 2,
isFavorite = false
)
private var favoriteStation = SimpleStation(
id = 1,
isFavorite = true
)
private val stationsList = listOf(ordinaryStation, favoriteStation)
@Test
fun getAllStations() = runBlockingTest {
simpleStationDao.insertAll(stationsList)
val databaseStations = simpleStationDao.getAllStations()
assert(databaseStations == stationsList)
}
@Test
fun getFavorites() = runBlockingTest {
simpleStationDao.insertAll(stationsList)
val databaseFavorites = simpleStationDao.getFavorites()
val favoritesList = mutableListOf<SimpleStation>()
for (item in stationsList) {
if (item.isFavorite) {
favoritesList.add(item)
}
}
assert(databaseFavorites == favoritesList)
}
}
P.S. CacheDatabaseTest has Before and After methods to create/close database and to access DAOs.
Aucun commentaire:
Enregistrer un commentaire