问题描述
我在写测试活动(我的测试类扩展ActivityInstrumentationTestCase2),我已经有了一些基本的测试,笔试和做工精细。
I'm writing tests for an activity (my test class extends ActivityInstrumentationTestCase2), I've got some basic tests written and working fine.
它完成通过的setResult(结果code,意图i)返回额外的数据到调用活动时,我想使用仪器让我的活动来完成做不过我的活动,然后检查它通过在的setResult电话。
However my activity when it completes returns extra data to the calling activity via setResult(resultcode, Intent i) What I'd like to do using instrumentation get my activity to finish, then check what it passed in the setResult call.
有一些提供框架,这样做的方法是什么?我一直没能找到任何东西,一种方法是子类的活动类,并且覆盖的setResult把它记住和放大器;揭露什么传递给的setResult(原来的setResult是最终的,所以你不能做到这一点无论是),好像应该有一个更好的办法。
Is there some framework provided way of doing this? i haven't been able to find anything yet, one approach would be to subclass the activity class and override setResult to have it remember & expose what was passed to setResult (turns out setResult is final, so you can't do this either), it seems like there should be a better way.
推荐答案
正如我在另questiom回答,你也可以使用的的 的和阴影下的测试活动。然后,ShadowActivity为您提供的方法可以轻松地知道,如果一个活动是整理和获取它的结果code。
As I answered in another questiom, you could also use Robolectric and shadow the Activity under test. Then, ShadowActivity provides you with methods to easily know if an Activity is finishing and for retrieving its result code.
作为一个例子,我的测试之一是这样的:
As an example, one of my tests looks like this:
@Test
public void testPressingFinishButtonFinishesActivity() {
mActivity.onCreate(null);
ShadowActivity shadowActivity = Robolectric.shadowOf(mActivity);
Button finishButton = (Button) mActivity.findViewById(R.id.finish_button);
finishButton.performClick();
assertEquals(DummyActivity.RESULT_CUSTOM, shadowActivity.getResultCode());
assertTrue(shadowActivity.isFinishing());
}
这篇关于测试一个活动叫的setResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!