首页 > 解决方案 > 从阴影方法调用真实对象的方法会导致无限递归

问题描述

我创建了一个调用真实对象的影子类,如http://robolectric.org/extending中所述:

@Implements(View::class)
class MyShadowView {

    @RealObject
    private lateinit var realView: View

    @Implementation
    fun animate(): ViewPropertyAnimator {
        return realView.animate() // this call ends up calling my shadow's animate() function recursively
    }
}

但是,当我的影子方法被执行时,它会导致无限递归。

我究竟做错了什么?

(我使用的是 Robolectric 4.2。)

标签: androidunit-testingmockingandroid-testingrobolectric

解决方案


根据http://robolectric.org/javadoc/4.1/org/robolectric/shadow/api/Shadow.html它应该是这样的:

    @Implementation
    fun animate(): ViewPropertyAnimator {
        Shadow.directlyOn(realView, View::class.java).animate()
    }

推荐阅读