Skip to content

Instantly share code, notes, and snippets.

@alediaferia
Created December 16, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alediaferia/c2a98b196707e5202195cbc1395e03fe to your computer and use it in GitHub Desktop.
Save alediaferia/c2a98b196707e5202195cbc1395e03fe to your computer and use it in GitHub Desktop.
A working version of the test with LiveData
@RunWith(AndroidJUnit4::class)
class PostDaoReadWriteTest {
private lateinit var postDao: PostDao
private lateinit var db: TestDatabase
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context, TestDatabase::class.java).build()
postDao = db.getPostDao()
}
@After
@Throws(IOException::class)
fun closeDb() {
db.close()
}
@Test
@Throws(Exception::class)
fun writePostAndReadInList() {
postDao.getAll().observeOnce {
assertEquals(0, it.size)
}
val post = TestUtil.createPost(id=42)
postDao.insert(post)
postDao.getAll().observeOnce {
assertEquals(1, it.size)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment