Robolectric buildActivity()与间谍的Mockito? [英] Robolectric buildActivity() with Mockito spy?

查看:577
本文介绍了Robolectric buildActivity()与间谍的Mockito?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,建设有Robolectric的生命周期公用事业的活动单元测试(从 Robolectric.buildActivity())和刺探同一活动与间谍的Mockito是相互排斥的。

It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity()) and spying on the same Activity with a Mockito spy are mutually exclusive.

由于 buildActivity()控制活动对象的构造,唯一的地方加入间谍的活动是在调用后 buildActivity() 。然而,间谍不能正常时,它在事后增加的功能。

Because buildActivity() controls the construction of the Activity object, the only place to add a spy for the Activity is after calling buildActivity(). However, the spy doesn't function properly when it's added after the fact.

间谍的 ActivityController 生命周期方法的副作用,如创建(),<$时,这一点尤其如此C $ C>开始()和简历()。我想这是因为ActivityController包含对后来添加的真​​正的活动对象,而不是间谍的参考。

This is especially true when spying for side effects of ActivityController lifecycle methods such as create(), start() and resume(). I assume this is because the ActivityController holds a reference to the "real" Activity object and not the spy that was added later.

那么,有什么办法可以窥探该公司正在单元Robolectric测试活动,使得间谍呼吁通过Robolectric的 ActivityController 生命周期方法时正常工作?

So is there any way to spy an Activity that's being unit tested with Robolectric, such that the spy works properly when calling the lifecycle methods via Robolectric's ActivityController?

推荐答案

目前至少对于其中活性不被测对象,但只有一个虚设活性哪个被测承载的片段的情况下,也能够注入模拟到测试活动,可以验证通过片段和活性之间的通信接口的活性相互作用(以下 http://developer.android.com/training/basics/fragments/communicating.html )。

At least for the case where the activity is not the object under test, but only a dummy activity which hosts a fragment under test, it is possible to inject a mock into the test activity which can verify interactions with the activity via the communication interface between fragment and activity (following http://developer.android.com/training/basics/fragments/communicating.html).

这篇关于Robolectric buildActivity()与间谍的Mockito?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆