android - org.mockito.exceptions.verification.NoInteractionsWanted:单元测试失败 - MVI 架构
问题描述
我已经为 android 应用程序实现了 MVI 架构,但我无法编写任何成功的测试。
这是一个例子:
fun verifyLoadStateSuccess() {
val loading= State(isLoading = true)
val loaded = State(list)
whenever(listUseCase.loadList()).thenReturn(Observable.just(list))
viewModel.dispatch(Action.Load)
testSchedulerRule.triggerActions()
inOrder(testObserver) {
verify(testObserver).onChanged(loading)
verify(testObserver).onChanged(loaded)
}
verifyNoMoreInteractions(testObserver) // if i comment this line, the test pass ok
}
我得到的错误是说:
No interactions wanted here:
-> at com.nhaarman.mockito_kotlin.MockitoKt.verifyNoMoreInteractions(Mockito.kt:258)
But found this interaction on mock 'observer':
-> at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
***
For your reference, here is the list of all invocations ([?] - means unverified).
1. [?]-> at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
我已经用调试器运行了代码,并检查了reducer
多少次被调用以及使用什么值state
,但只有两次具有正确的值。
我不明白considerNotify
它说它被调用的那个方法是什么,我怎样才能将它添加到我的测试中。
解决方案
这意味着您的观察者 onChanged 正在被调用更多事件。在运行测试时,在实时数据的 thinkNotify 方法中设置一个断点。您将看到比您认为的更多的事件被触发,因此测试失败。
推荐阅读
- python - 从标签中获取href链接
- c# - JWT 不授权角色,即使用户拥有角色
- node.js - PaymentIntent 与基于订阅的产品
- java - 根据 BigDecimal 上的索引删除字符
- python - TypeError: 'int' object is not subscriptable - Trying To Create A Graph
- html - 是否所有元素都“应用”了所有属性?
- c++ - 对 Maya 插件 (v2016.5) 的此构建错误进行故障排除的困难
- javafx - 如何在具有特定关键帧的同一点暂停和启动时间线
- javascript - 自定义选择菜单中的链接功能中断
- unity3d - VR 耳机问题:由于默认着色器,对象显示在一只眼睛中。(在我的情况下是 TextMeshPro 着色器)