只是想知道其他人的意见我有两种方法可以做某事并且很好奇哪个更好(希望你为什么这么认为)
我有 2 个文件 WordRepository 和 WordViewModel。我可以在 Repo 或 ViewModel 中以两种方式执行协程,但希望有人能告诉我为什么我会在其中一个或另一个中执行协程,反之亦然。
版本 A.(协程在 Repo 中的位置)
WordRepo:
class WordRepository(private val wordDao: WordDao): WordRepo {
@WorkerThread
override suspend fun deleteAllLogsOlderThan(XDays: Int): Int = withContext(IO) {
return@withContext wordDao.deleteAll()
}
}
WordViewModel:
class WordViewModel(private val wordRepository: WordRepo) : ViewModel() {
fun deleteAllLogsOlderThanA(XDays:Int): Int = runBlocking {
wordRepository.deleteAllLogsOlderThan(XDays)
}
}
Run Code Online (Sandbox Code Playgroud)
版本 B.(协程在 ViewModel 中的位置)
Word Repo:
class WordRepository(private val wordDao: WordDao): WordRepo {
@WorkerThread
override suspend fun deleteAllLogsOlderThan(XDays: Int): Int = wordDao.deleteAll()
}
WordViewModel:
class …Run Code Online (Sandbox Code Playgroud) 我在不需要robolectric的情况下无法创建单元测试。我在代码中使用AndroidThreeTen.init(this),如果我禁用了robolectric,则在运行测试时出现错误:
org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered
如果我将其保持启用状态,则会得到以下信息:
[Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY
我试过使用testImplementation'com.jakewharton.threetenabp:threetenabp:1.1.0'没什么区别。我在应用程序和testApplication中调用了AndroidThreeTen.init(this)。有任何想法吗?这是我的考验
@Test
fun `on Calling function i it returns an Int`() {
assertThat("Returned class is not an Int", Log.i("Test", "Test"), isA(Int::class.java))
assertThat("Returned Int is not 0", Log.i("Test", "Test"), `is`(0))
}
Run Code Online (Sandbox Code Playgroud)
还是因为这个原因我必须使用robolectric吗?(附带说明:日志不是来自android的util.log,而是我自己的类)(已编辑)
android ×2
kotlin ×2
android-room ×1
coroutine ×1
mvvm ×1
robolectric ×1
testing ×1
unit-testing ×1