我正在尝试以下课程。我遇到问题的方法是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()可能会丢失。 正确存根的示例: when(mock.isOk())。thenReturn(true); when(mock.isOk())。thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); 提示: 1.缺少thenReturn() 2.您正在尝试存根不支持的最终方法 3:如果要在'thenReturn'指令完成之前在其中暂存另一个模拟的行为
答案 0 :(得分:1)
出现错误的行应该是:
doNothing().`when`(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)
就像来自Javadoc here的示例一样:
List list = new LinkedList();
List spy = spy(list);
//let's make clear() do nothing
doNothing().when(spy).clear();
spy.add("one");
//clear() does nothing, so the list still contains "one"
spy.clear();