【发布时间】:2019-01-15 07:58:52
【问题描述】:
我正在尝试测试以下课程。我遇到问题的方法是showScollerView,因为我试图对行为进行存根/模拟,然后在测试中验证行为。
class CustomScrollerView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
styleAttributes: Int = 0)
: ConstraintLayout(context, attributeSet, styleAttributes) {
private var fragment: ConstraintLayout by Delegates.notNull()
private var layoutResEnding: Int = 0
private val transition = ChangeBounds()
private val constraintSet = ConstraintSet()
private var isShowing = false
init {
View.inflate(context, R.layout.overview_scroller_view, this)
transition.interpolator = AccelerateInterpolator()
transition.duration = 300
}
fun <L: ConstraintLayout> setView(view: L) {
fragment = view
}
fun setLayoutResourceFinish(@LayoutRes id: Int) {
layoutResEnding = id
}
fun showScrollerView() {
constraintSet.clone(context, layoutResEnding)
TransitionManager.beginDelayedTransition(fragment, transition)
constraintSet.applyTo(fragment)
isShowing = true
}
fun isScrollViewShowing() = isShowing
}
这是测试类
class CustomScrollerViewTest: RobolectricTest() {
@Mock
lateinit var constraintSet: ConstraintSet
@Mock
lateinit var constraintLayout: ConstraintLayout
private var customScrollerView: CustomScrollerView by Delegates.notNull()
@Before
fun setup() {
customScrollerView = CustomScrollerView(RuntimeEnvironment.application.baseContext)
}
@Test
fun `test that CustomScrollerView is not null`() {
assertThat(customScrollerView).isNotNull()
}
@Test
fun `test that the scrollerView is shown`() {
doNothing().`when`(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)) /* Error here */
doNothing().`when`(constraintSet).applyTo(constraintLayout)
customScrollerView.setLayoutResourceFinish(R.layout.fragment)
customScrollerView.setView(constraintLayout)
customScrollerView.showScrollerView()
assertThat(customScrollerView.isScrollViewShowing()).isEqualTo(true)
verify(constraintSet).applyTo(constraintLayout)
verify(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)
}
}
我在这一行得到错误:
doNothing().when(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment))
这是实际的错误信息:
此处检测到未完成的存根: -> 在 com.nhaarman.mockito_kotlin.MockitoKt.doNothing(Mockito.kt:108)
例如thenReturn() 可能会丢失。 正确的存根示例: 当(模拟。isOk())。然后返回(真); when(mock.isOk()).thenThrow(异常); doThrow(exception).when(mock).someVoidMethod(); 提示: 1. 缺少 thenReturn() 2.您正在尝试存根不支持的最终方法 3:如果完成,您将在“thenReturn”指令之前存根另一个模拟的行为
【问题讨论】: