- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是测试新手,我想学习如何使用 MVVM 模式测试协程。我刚刚关注了 https://github.com/android/architecture-samples 项目并做了一些更改(删除了远程源)。但是在测试 ViewModel 以从存储库中获取数据时,它一直失败并出现此错误。
value of : iterable.size()
expected : 3
but was : 0
iterable was: []
Expected :3
Actual :0
下面是我的 ViewModel
测试类,不知道我遗漏了什么。此外,在模拟存储库时,我可以在打印 taskRepository.getTasks()
时从中获得预期结果,它只是在调用 loadTasks( )
@ExperimentalCoroutinesApi
@RunWith(MockitoJUnitRunner::class)
class TasksViewModelTest {
private lateinit var tasksViewModel: TasksViewModel
val tasksRepository = mock(TasksRepository::class.java)
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = TestMainCoroutineRule()
// Executes each task synchronously using Architecture Components.
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@Before
fun setupViewModel() {
tasksViewModel = TasksViewModel(tasksRepository)
}
@Test
fun whenLoading_hasListOfTasks() = runBlockingTest {
val task1 = Task("title1", "description1")
val task2 = Task("title2", "description2")
val task3 = Task("title3", "description3")
`when`(tasksRepository.getTasks()).thenReturn(Result.Success(listOf(
task1,
task2,
task3
)))
tasksViewModel.loadTasks()
val tasks = LiveDataTestUtil.getValue(tasksViewModel.tasks)
assertThat(tasks).hasSize(3)
}
}
class TasksViewModel @Inject constructor(
private val repository: TasksRepository
) : ViewModel() {
private val _tasks = MutableLiveData<List<Task>>().apply { value = emptyList() }
val tasks: LiveData<List<Task>> = _tasks
fun loadTasks() {
viewModelScope.launch {
val tasksResult = repository.getTasks()
if (tasksResult is Success) {
val tasks = tasksResult.data
_tasks.value = ArrayList(tasks)
}
}
}
}
下面列出了帮助类,我只是从示例项目中复制了相同的类。
object LiveDataTestUtil {
/**
* Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
* Once we got a notification via onChanged, we stop observing.
*/
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data[0] = o
latch.countDown()
liveData.removeObserver(this)
}
}
liveData.observeForever(observer)
latch.await(2, TimeUnit.SECONDS)
@Suppress("UNCHECKED_CAST")
return data[0] as T
}
}
@ExperimentalCoroutinesApi
class TestMainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
最佳答案
事实证明这是 mockito 的问题,我有一个旧版本,我发现有一个名为 mockito-kotlin
的库可以简化测试协程,如 here 所述.然后我将我的代码修改为这个,它运行良好。
tasksRepository.stub {
onBlocking { getTasks() }.doReturn(Result.Success(listOf(task1, task2, task3)))
}
关于android - 测试协程和 LiveData : Coroutine result not reflected on LiveData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534129/
import kotlinx.coroutines.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
我有一个长计算的外部库。这个库就是一切,但在过早停止方面是合作的。它被包装并在一个协程中启动。 我想从调用方终止进程。协程取消是合作的,所以它不起作用。有没有办法突然终止协程? 最佳答案 非挂起的协程
我是 Kotlin/Coroutines 的新手,我注意到两种不同的使用方式 CoroutineScope . 选项 1 如下,在任何函数中: CoroutineScope(Dispatchers.D
我对协同例程比较陌生,我正在尝试获取 Launch 协同例程将完成的行为: launch(UI) { val v1 = someDeferredType val v2 = v1.a
文档说: @asyncio.coroutine Decorator to mark generator-based coroutines. This enables the generator use
我在 kotest 中使用了 Funspec 测试风格,我得到了一个由框架自动注入(inject)的 coroutineScope,如下所示。 class MyTestSpec: FunSpec()
我想尝试从一个courtine 中暂停主线程。几个问题,但首先让我向您展示我正在使用的代码块作为测试: class MainActivity : Activity(), CoroutineScope
我不确定我是否理解 purescript-coroutines 中有一个 connect 和一个 pullFrom 函数的原因。 ,以及何时使用它们。看看这些类型,它们似乎已经习惯了改变“通信方向”(
我在不同的类(class_A、class_B)中有 2 个协程(co_A、co_B)由调度程序定期调用。在执行期间的某个时候,co_B 需要一个 co_A 在其运行期间正在计算的结果。 我想做的是这样
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
目前在 pzmq 中有多种方法来实现文档中提到的异步 IO 循环。 http://pyzmq.readthedocs.io/en/latest/api/index.html 从纯性能的角度来看,文档中
看来我误解了当有许多套接字被打开时 Windows 如何在 TIME_WAIT 中处理套接字。如果在 TIME_WAIT 中挂出太多,它只会出错。 Linux 清理旧连接并成功(至少在我的盒子上,不确
我正在尝试对我使用 Kotlin 协程制作的 Presenter 进行单元测试,这也是我第一次使用 Mockito 每当我尝试运行单元测试时,它在协程中第一次尝试对我的 View 执行任何操作时都会收
我注意到 Boost.Coroutine2 for C++ 中删除了对对称协程的支持,所以我想了解 - 两者之间有什么区别? 最佳答案 Ana Lúcia de Moura 和 Roberto Ier
在Linux System Programming, 2nd Edition书中,协程与纤维之间的差异解释如下: 协程和纤维提供了比线程更轻的执行单元(前者是编程语言结构时的名称,后者是系统结构时的名
此示例已在另一个问题中用于说明如何使用协程来编写视频游戏中的过场动画: bob.walkto(jane) bob.lookat(jane) bob.say("How are you?") wait(2
我正在寻找实现 FSM 的方法,这导致我第一次遇到协程。 我看到了几个示例(here、here 和 here),它们暗示状态机可以由单个协程实现。然而,我注意到所有这些机器的共同点是,不包括循环,它们
我试图从ViewModel异步获取所有可启动的已安装应用程序。 这是我的ViewModel类的样子: class AppInstalledViewModel(application: Applicat
如何在kotlin中像多线程一样进行多次启动 我想让 first second 永远同时工作!! 喜欢这段代码... runBlocking { // first launch{
最近我正在按照这个 CodeLabs tutorial 学习 kotlin-coroutine 。经过一些实践后,我想知道我是否可以在 java 中编写相同的代码。所以首先我在 MyKotlinFra
我是一名优秀的程序员,十分优秀!