我是android-x的新手,并且是第一次使用Room。这似乎应该是简单的。但是我的设置和测试遇到了一些问题。
我的单元测试失败
错误: java.lang.AssertionError:
预期:1
Actual:null
我希望有人能够提供帮助。不知道还要添加什么(SO要求我添加更多内容才能发布此笑)。因此希望有人能看到我要去哪里了。
class LogTest {
//setup db
private lateinit var logDao: LogDao
private lateinit var testAppDatabase: AppDatabase
@Before
fun setUp() {
val context = ApplicationProvider.getApplicationContext<Context>()
testAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).allowMainThreadQueries().build()
logDao = testAppDatabase.getLogDao()
}
@After
@Throws(IOException::class)
fun closeDb() {
testAppDatabase.close()
}
@Test
@Throws(Exception::class)
fun `log Database is Empty on Start`() {
assertEquals(null, logDao.getAllLogs().value?.size)
val testLog = LogModel("Test", "Test", 0, 0, 0.0, 0.0)
logDao.insertAll(testLog)
assertEquals(1, logDao.getAllLogs().value?.size)
}
}
@Entity(tableName = "Logs")
data class LogModel (
@ColumnInfo(name = "tag") var tag: String,
@ColumnInfo(name = "msg") var msg: String,
@ColumnInfo(name = "device_id") var deviceID: Int,
@ColumnInfo(name = "user_id") var userID: Int,
@ColumnInfo(name = "latitude") var latitude: Double,
@ColumnInfo(name = "longitude") var longitude: Double
) {
@PrimaryKey(autoGenerate = true) var id: Long? = null
//@ColumnInfo(name = "timestamp") var timestamp: String? = null
}
@Dao
interface LogDao {
@Query("SELECT * FROM Logs")
fun getAllLogs(): LiveData<List<LogModel>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg logs: LogModel)
}
@Database(entities = [LogModel::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
//Database to be initialised here
abstract fun getLogDao(): LogDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"My_Database"
).build()
INSTANCE = instance
return instance
}
}
}
}
答案 0 :(得分:0)
我认为您应该阻塞正在运行的线程,直到插入操作完成为止
您可以在<body ng-controller="appController as vm">
<p ng-if=" ̶$̶r̶o̶o̶t̶S̶c̶o̶p̶e̶.̶ timeoutFlag">Button Clicked</p>
<button id="clickmeBtn">Click me!</button>
周围的协程中使用$rootScope
或在插入完成后尝试致电runBlocking{}
。