代码之家  ›  专栏  ›  技术社区  ›  ant2009

返回void返回类型的模拟存根

  •  0
  • ant2009  · 技术社区  · 7 年前

    我有下面一节课要考。我遇到问题的方法是 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.mockito kt.doNothing上(mockito.kt:108)

    E、 g.thenReturn()可能丢失。 正确的短截线示例: 当(mock.isOk()).thenReturn(true); 当(mock.isOk()).thenThrow(异常); doThrow(异常).when(mock).someVoidMethod(); 提示: 一。缺少thenReturn() 2。您正在尝试存根一个不受支持的final方法 3: 如果完成了“thenReturn”指令,则在“thenReturn”指令之前,您正在破坏内部另一个模拟的行为

    1 回复  |  直到 7 年前
        1
  •  2
  •   David Rawson maruti060385    7 年前

    出现错误的行应该是:

    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();