Created
December 16, 2018 18:12
-
-
Save alediaferia/c2a98b196707e5202195cbc1395e03fe to your computer and use it in GitHub Desktop.
A working version of the test with LiveData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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