Skip to content

Instantly share code, notes, and snippets.

@reactivedroid
Created July 30, 2019 12:17
Show Gist options
  • Save reactivedroid/9451a743bdfd767b6159600811130408 to your computer and use it in GitHub Desktop.
Save reactivedroid/9451a743bdfd767b6159600811130408 to your computer and use it in GitHub Desktop.
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class FavoriteShowsRepositoryTest {
@Mock
private lateinit var context: Context
private lateinit var favoriteShowsRepository: FavoriteShowsRepository
private lateinit var tvMazeDb: TvMazeDatabase
private lateinit var showDao: ShowDao
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
tvMazeDb = Room.inMemoryDatabaseBuilder(
context, TvMazeDatabase::class.java
).build()
showDao = tvMazeDb.showDao()
favoriteShowsRepository = FavoriteShowsRepository(showDao)
}
@Test
fun testShowInsertionInDb() {
runBlocking {
favoriteShowsRepository.insertIntoFavorites(TestUtil.getFakeShow())
val favShows = favoriteShowsRepository.allFavoriteShows()
assertThat(favShows.isNotEmpty()).isTrue()
}
}
@Test
fun testRemoveFromDb() {
runBlocking {
favoriteShowsRepository.clearAll()
assertThat(favoriteShowsRepository.allFavoriteShows().isEmpty()).isTrue()
}
}
@Test
fun testFavoriteShows() {
runBlocking {
val fakeShow = TestUtil.getFakeShow()
favoriteShowsRepository.insertIntoFavorites(fakeShow)
val favoriteShows = favoriteShowsRepository.allFavoriteShows()
assertThat(favoriteShows[0] == fakeShow).isTrue()
}
}
@After
@Throws(IOException::class)
fun closeDb() {
tvMazeDb.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment